I have an existing json file that I need to transform, but am having trouble with it.
Here’s an example of the structure:
jq -r '.document.results.summary.run.[] | [.run_number, .cinfo] ' input.json
[
"42",
[
{
"name": "Joe shmo",
"state": "AZ",
"city": "Phoenix"
},
{
"name": "Jane shmo",
"state": "CA",
"city": "Blythe"
]
]
[
"84",
[
{
"name": "Jake shmo",
"state": "NY",
"city": "Albany"
},
{
"name": "Jay shmo",
"state": "TN",
"city": "Franklin"
]
]
I need to convert this to a CSV to look like:
"run_number","cinfo.name","cinfo.state","cinfo.city"
Example:
"42","Joe shmo","AZ","Phoenix"
"42","Jane shmo","CA","Blythe"
"84","Jake shmo","NY","Albany"
"84","Jay shmo","TN","Franklin"
I tried to use | @csv I get:
jq: error (at input.json:1397): array ([{"@name":"...) is not valid in a csv row
I believe that this is because it is an array of multiple indexes. How can I transform the output using jq?
2
Answers
You tried applying
@csv
to an array containing non-strings. You have to decompose them to get to the values.If all goes well (I could have tested it if you had provided
input.json
), this should produceYou don’t have a valid JSON file in your code sample. Perhaps you meant to write this:
In this case, you can use the command:
and you will receive this result: