-1

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!

peak
  • 105,803
  • 17
  • 152
  • 177
Benjamin
  • 101
  • 1
  • 8
  • 2
    We encourage questioners to show what they have tried so far to solve the problem themselves. – Cyrus Mar 04 '23 at 20:14
  • 1
    Does this answer your question? [Convert json to csv using jq, with array nested within array](https://stackoverflow.com/questions/64859291/convert-json-to-csv-using-jq-with-array-nested-within-array) – RomanPerekhrest Mar 04 '23 at 20:16
  • Not 100%, I think I was more stuck in working with elements that are the same but the type is diffrent (arrays vs objects) – Benjamin Mar 05 '23 at 18:56

1 Answers1

1

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)"
pmf
  • 24,478
  • 2
  • 22
  • 31