I have a JSON payload that I'm looking to convert to CSV that looks like the below:
[
{
"endpoint": "APPLE",
"date": "2022-11-02 12:00",
"upUsage": 0,
"downUsage": 18000,
"upAvgRate": 0,
"downAvgRate": 600,
"upMaxRate": 0,
"downMaxRate": 800
},
{
"endpoint": "BANANA",
"date": "2022-11-02 12:00",
"upUsage": 0,
"downUsage": 17600,
"upAvgRate": 0,
"downAvgRate": 587,
"upMaxRate": 0,
"downMaxRate": 693
},
{
"endpoint": "CARROT",
"date": "2022-11-02 12:00",
"upUsage": 0,
"downUsage": 8000,
"upAvgRate": 0,
"downAvgRate": 533,
"upMaxRate": 0,
"downMaxRate": 533
}
]
I am trying to convert this to a standard CSV file with the appropriate headers via jq, but having difficulties in doing so. Below is my desired output:
"endpoint","date","upUsage","downUsage","upAvgRate","downAvgRate","upMaxRate","downMaxRate"
"APPLE","2022-11-02 12:00",0,18000,0,600,0,800
"BANANA","2022-11-02 12:00",0,17600,0,587,0,693
"CARROT","2022-11-02 12:00",0,8000,0,533,0,533
I've been able to use the below jq to get close to this output, but my headers are not being included:
cat testJson.json | jq -r '.[] | join(",")'
*Note: - There are also instances in which one of my JSON objects may not include the same number of values, so I need my output file to account for this and simply enter a null value between the commas to keep a consistent number of columns