How to fix duplicated output of jq flattening JSON array

后端 未结 1 1569
攒了一身酷
攒了一身酷 2021-01-27 09:34

I am trying to flatten a JSON file by using jq command. But the output got duplicated.

Please see my jqplay here: https://jqplay.org/s/gwvMIH_fed

My input JSON:<

相关标签:
1条回答
  • 2021-01-27 09:54

    Each .[] is like a "for" loop, so the multiplicative behavior you observe is essentially the result of having nested for loops. It would seem that what you want is closer to:

    .value[] as $v
    | $v.timeseries[].data[] as $d
    | {"apiId": $v.id,
      "metrics": $v.name.value,
      "timestamp": $d.timeStamp,
      "value": $d.average }
    

    With your JSON as input, this produces two JSON objects, though the second of these differs very slightly from what you give as the expected output.

    0 讨论(0)
提交回复
热议问题