David Soroko David Soroko - 3 months ago 37
JSON Question

Using jq to "normalize" JSON

I am trying to use jq to "normalize" JSON so that given the input:

[
[
{"M":6, "C":66, "R":0.1},
{"M":6, "C":81, "R":0.9}
],
[
{"M":11, "C":94, "R":0.8},
{"M":11, "C":55, "R":0.46}
]
,
...
]


the output should be:

[
{
"M" : 6,
"X" : [{"66" : 0.1},{"81": 0.9}]
},
{
"M" : 11,
"X" : [{"94" : 0.8},{"55": 0.46}]
},
...

]


I can extract M with
map({M: .[0].M
but not sure how to proceed

Answer
[foreach .[] as $list ({};
  {
    "M": $list[0].M,
    "X": [foreach $list[] as $item ({};
            {"\($item.C)": $item.R}
         )]
  }
)]
Comments