将单个键数组合并为值数组



我的原始json:

{
   columns:["colA","colB","colC"]
   values: [
       ["3","abc",200],
       ["4","def",300],
       ["5","ghi",400],
       ["6","jkl",500]
   ]
}

我想生产:

[
    {colA: 3, colB: "abc", colC: 200},
    {colA: 4, colB: "def", colC: 300},
    {colA: 5, colB: "ghi", colC: 400},
    {colA: 6, colB: "jkl", colC: 500}
]

如果可能的话,我似乎根本找不到正确的方法。

谢谢

输入不是很有效的 JSON,但一旦修复,您可以使用 jq 食谱 (https://github.com/stedolan/jq/wiki/Cookbook( 中的def objectify

# objectify/1 expects an array of atomic values as inputs, and packages
# these into an object with keys specified by the "headers" array and
# values obtained by trimming string values, replacing empty strings
# by null, and converting strings to numbers if possible.
def objectify(headers):
  def tonumberq: tonumber? // .;
  def trimq: if type == "string" then sub("^ +";"") | sub(" +$";"") else . end;
  def tonullq: if . == "" then null else . end;
  . as $in
  | reduce range(0; headers|length) as $i
      ({}; .[headers[$i]] = ($in[$i] | trimq | tonumberq | tonullq) );

有了这个def,"主"jq滤波器很简单:

.columns as $headers
| .values
| map(objectify($headers))

最新更新