I have the following JSON that I want to convert to CSV. It is an array of arrays, which contains an object and an array of objects.
[
[
{
"name": "A",
"DATA": [
{
"V": 192.1,
"Q": 0,
"T": "2020-06-12T16:52:10.000Z"
},
{
"V": 175.2,
"Q": 0,
"T": "2020-06-12T16:53:50.000Z"
}
]
},
{
"name": "B",
"DATA": [
{
"V": 176,
"Q": 0,
"T": "2020-06-12T16:53:25.000Z"
},
{
"V": 122.8,
"Q": 0,
"T": "2020-06-12T16:53:30.000Z"
},
{
"V": 122.01,
"Q": 0,
"T": "2020-06-12T16:55:35.000Z"
}
]
}
]
]
The desired is the following:
A,192.1,0,2020-06-12T16:52:10.000Z
A,175.2,0,2020-06-12T16:53:50.000Z
B,176,0,2020-06-12T16:53:25.000Z
B,122.8,0,2020-06-12T16:53:30.000Z
B,122.01,0,2020-06-12T16:55:35.000Z
My output is generating the product of name
and DATA
, which is not what I need.
.[][] | [.name] + (.DATA[] | [.V,.Q,.T]) | @csv
If the keys in elements of DATA
are always in the same order, you can use the following instead.
.[][] | [.name] + (.DATA[] | map(.)) | @csv
To get a valid CSV output, you need to invoke JQ with -r/--raw-output option.