简体   繁体   English

jq如何合并多个数组?

[英]jq how to merge multiple arrays?

I have the following data, the output of multiple jq pipes:我有以下数据,多个jq管道的输出:

 [
   {
     "Russia": 1073849
   }
 ]
 [
   {
     "Spain": 593730
   }
 ]
 [
   {
     "France": 387252
   }
 ]
 [
   {
     "UK": 371125
   }
 ]

My desired output is:我想要的输出是:

 [
   {
     "Russia": 1073849
   },
   {
     "Spain": 593730
   },
   {
     "France": 387252
   },
   {
     "UK": 371125
   }
 ]

Based on similar questions I tried '.[]|transpose|map(add)' and it gives an error: Cannot index object with number .基于类似的问题,我尝试了'.[]|transpose|map(add)'并且它给出了一个错误: Cannot index object with number Also I cannot group_by(key) because there is no common key in the objects.我也不能group_by(key)因为对象中没有公共键。

If I understand correctly, you want to produce an array as output.如果我理解正确,您想生成一个数组作为输出。 You can move your array wrapper from around the final object to the entire jq call to do this:您可以将数组包装器从最终对象周围移动到整个jq调用来执行此操作:

curl -s https://corona-stats.online/\?format\=json | 
jq '[ .data[] 
      | select(.continent | test("Europe"))
      | {(.country): .cases} 
    ]'

Output after | [0:4]输出后| [0:4] | [0:4] : | [0:4] :

[
  {
    "Russia": 1073849
  },
  {
    "Spain": 603167
  },
  {
    "France": 395104
  },
  {
    "UK": 374228
  }
]

If you want an object, you could use:如果你想要一个对象,你可以使用:

curl -s https://corona-stats.online/\?format\=json | 
jq '[ .data[]
      | select(.continent | test("Europe"))
    ] 
    | map({(.country): .cases}) 
    | add'

or:要么:

curl -s https://corona-stats.online/\?format\=json | 
jq '[ .data[]
      | select(.continent | test("Europe"))
    ]
    | reduce .[] as $e ({}; .[$e.country] = $e.cases)'

Output:输出:

{
  "Russia": 1073849,
  "Spain": 603167,
  "France": 395104,
  "UK": 374228,
  "Italy": 289990,
  "Germany": 264375,
  "Ukraine": 159702,
  "Romania": 105298,
  "Belgium": 94306,
  "Sweden": 87345,
  "Netherlands": 84778,
  "Poland": 75134,
  "Belarus": 74552,
  "Portugal": 65021,
  "Switzerland": 47751,
  "Moldova": 43734,
  "Czechia": 38187,
  "Austria": 34305,
  "Serbia": 32511,
  "Ireland": 31549,
  "Bosnia": 23929,
  "Denmark": 20571,
  "Bulgaria": 18061,
  "Macedonia": 15925,
  "Hungary": 13879,
  "Croatia": 13749,
  "Greece": 13730,
  "Norway": 12330,
  "Albania": 11672,
  "Finland": 8725,
  "Luxembourg": 7244,
  "Montenegro": 6900,
  "Slovakia": 5768,
  "Slovenia": 3831,
  "Lithuania": 3397,
  "Estonia": 2722,
  "Malta": 2454,
  "Iceland": 2174,
  "Latvia": 1482,
  "Andorra": 1438,
  "San Marino": 723,
  "Channel Islands": 639,
  "Faroe Islands": 428,
  "Isle of Man": 339,
  "Gibraltar": 334,
  "Monaco": 177,
  "Liechtenstein": 111,
  "Holy See (Vatican City State)": 12
}

Although it doesn't matter on this dataset, I'd prefer using == "Europe" rather than test("Europe") which is a bit less precise.尽管在这个数据集上无关紧要,但我更喜欢使用== "Europe"而不是test("Europe") ,后者不太精确。

Assuming input.json file is:假设input.json文件是:

[{"Russia": 1073849}]
[{"Spain": 593730}]
[{"France": 387252}]
[{ "UK": 371125}]

Then this:然后这个:

jq -s 'reduce .[] as $x ([]; . + $x)' input.json

returns:返回:

[
  {
    "Russia": 1073849
  },
  {
    "Spain": 593730
  },
  {
    "France": 387252
  },
  {
    "UK": 371125
  }
]

Notes:笔记:

  • jq can merge arrays with the + operator, eg [1]+[2] returns [1,2] . jq可以使用+运算符合并数组,例如[1]+[2]返回[1,2]
  • The -s flag reads input.json and put all entries into an array. -s标志读取input.json并将所有条目放入数组中。 So you end up with an array of arrays that you can merge with reduce .所以你最终得到了一个可以与reduce合并的数组数组。

This can be simplified even further with:这可以进一步简化:

jq -s 'add' input.json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM