0

My json file is basically the same as this except it's all on one line. My goal is to obtain a .csv file like the possible output listed below. I obtain the json file with a curl command, would I put the jq command to obtain the .csv output and the end of the curl command? I would appreciate any guidance.

Input:

[
    {"code": "NSW", "name": "New South Wales", "level":"state", "country": "AU"},
    {"code": "AB", "name": "Alberta", "level":"province", "country": "CA"},
    {"code": "ABD", "name": "Aberdeenshire", "level":"council area", "country": "GB"},
    {"code": "AK", "name": "Alaska", "level":"state", "country": "US"}
]

Possible output:

code,name,level,country
NSW,New South Wales,state,AU
AB,Alberta,province,CA
ABD,Aberdeenshire,council area,GB
AK,Alaska,state,US
SuperStormer
  • 4,997
  • 5
  • 25
  • 35
newbie
  • 11
  • 1
  • Does this answer your question? [How to convert arbitrary simple JSON to CSV using jq?](https://stackoverflow.com/questions/32960857/how-to-convert-arbitrary-simple-json-to-csv-using-jq) – SuperStormer Oct 10 '21 at 21:15

1 Answers1

0

The article you are referring to has several examples of what you can do once you have the JSON in a local file. If you want to avoid creating an intermediate file, you could pipe the JSON from the curl command directly to jq, e.g. along the lines of:

curl ... | jq -fr myprogram.jq

However, in practice, blindly piping the output of curl in this way sometimes results in unexpected failures, notably as happens when the actual output produced by curl (on stdout) includes some kind of header non-JSON header. If such extra information cannot be avoided, then it can usually be removed by adding a text-processing command such as sed to the pipeline, e.g. along the lines of:

curl ... | sed 1,2d | jq -fr myprogram.jq
peak
  • 105,803
  • 17
  • 152
  • 177
  • json file has many strings [{},{},{},{}], within the strings {} are many objects: "STK_NUM":"1004251 ", "DLR_COST":40.32 , "ACE_RTL_AMT":9.99, How would I write the jq program to create the keys, STK_NUM, DLR_COST,ACE_RTL_AMT as headers in my csv output file with values listed as 1004251,40.32,9.99 for all strings inside [ ] ? – newbie Oct 10 '21 at 23:29
  • I would suggest you create a new question, following the [mcve] guidelines as much as possible, preferably including what you have tried. – peak Oct 11 '21 at 01:34