Search code examples
bashcsvjqexport-to-csvyq

jq json to csv with all child elements


For the following json I extracted from an XML

{
  "unit-type": "amp",
  "unit-id": "0",
  "cpu": [
    {
      "cpu-id": "0",
      "temperature": "64(C)"
    },
    {
      "cpu-id": "1",
      "temperature": "64(C)"
    }
  ]
}
{
  "unit-type": "bcp",
  "unit-id": "1",
  "cpu": {
    "cpu-id": "0",
    "temperature": "74(C)"
  }
}

I would like to have the following csv output

I only can use bash and the tools xq jq and yq

unit-type,unit-id,cpu-id,temperature
amp,0,0,64(C)
amp,0,1,64(C)
bcp,1,0,74(C)

I think my main issue I am running in, is that the 2th cpu element is a object, while the first cpu element is a array in JQ. I am hitting face against the wall for this one.

Thanks in advance!


Solution

  • Your input is a stream, therefore, to output the header just once, use the -n flag to have a single input to print from, then inputs to process all the actual inputs. Using arrays and objects, you can distinguish between the two cases for .cpu.

    jq -nr '
      ["unit-type","unit-id","cpu-id","temperature"] as $headers | $headers, (inputs
        | [.[$headers[0,1]]] + (.cpu | arrays[], objects | [.[$headers[2,3]]])
      ) | @csv
    '
    
    "unit-type","unit-id","cpu-id","temperature"
    "amp","0","0","64(C)"
    "amp","0","1","64(C)"
    "bcp","1","0","74(C)"