I have the following json contents:
{
"iccid": "1961021111937667",
"timeStamp": "2021-03-25T09:42:30.681Z",
"cycleStartDate": "2021-02-28T16:00:00.000Z",
"cycleEndDate": "2021-03-31T14:59:59.000Z",
"deviceCycleUsageInZones": {
"TEST-25GB, 1": [
{
"ratePlan": "TEST-25GB",
"ratePlanVersion": "1",
"zone": "default zone",
"dataUsage": 15155147646,
"dataUsageUnit": "bytes",
"voiceMTUsage": null,
"voiceMTUsageUnit": null,
"voiceMOUsage": null,
"voiceMOUsageUnit": null,
"smsmtusage": null,
"smsmousage": 3
}
],
"TEST-29GB, 1": [
{
"ratePlan": "TEST - 29602",
"ratePlanVersion": "1",
"zone": "default zone",
"dataUsage": null,
"dataUsageUnit": null,
"voiceMTUsage": 1080,
"voiceMTUsageUnit": "seconds",
"voiceMOUsage": 960,
"voiceMOUsageUnit": "seconds",
"smsmtusage": null,
"smsmousage": null
}
]
}
}
How to go through the contents of .json file to get the following .csv output?
1961021111937667, 2021-03-25T09:42:30.681Z,15155147646,bytes,null,null,null,null,null,3
1961021111937667, 2021-03-25T09:42:30.681Z,null,null,1080,seconds,960,seconds,null,null
In other words, I want the following headers included:
iccid,timeStamp,dataUsage,dataUsageUnit,voiceMTUsage,voiceMTUsageUnit,voiceMOUsage,voiceMOUsageUnit,smsmtusage,smsmousage
Assuming the input is structured as shown, the following is guaranteed to produce valid CSV (including the header), in accordance with the headers in the question:
["iccid","timeStamp"] as $h1
| ["dataUsage","dataUsageUnit","voiceMTUsage","voiceMTUsageUnit","voiceMOUsage","voiceMOUsageUnit","smsmtusage","smsmousage"] as $h2
| ($h1+$h2),
([.[$h1[]]] + (.deviceCycleUsageInZones[][] | [.[$h2[]]] ))
| @csv
If you really don't want the strings to be quoted, then change @csv
to join(",")
, and hope for the best.
The above solution does not depend on the ordering of keys within the JSON objects. This is achieved using the idiom:
.[ $index[] ]
where $index
is an array of key names. This tiny bit of cleverness is possible because, given a jq expression, s, producing a stream of JSON values s1, s2, ..., the jq expression $x[s]
evaluates to the stream: $x[s1], $x[s2], ..., assuming all the latter jq expressions are valid.