I would like to convert the following JSON into a csv format using jq. I know there are tons of similar questions but I could not figure it out based on them.
{
"one": {
"firstName": "John",
"lastName": "Smith",
"gender": "man",
"age": 32,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
}
},
"two": {
"firstName": "Johnny",
"lastName": "Smithy",
"gender": "man",
"age": 33,
"address": {
"streetAddress": "22 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
}
}
}
The output should look like the following. I'm struggeling with the nested object as value of the address key.
number,firstName,lastName,gender,age,streetAddress,city,state,postalCode
one,John, Smith,man, 32, 21 2nd Street, New York, NY, 10021
two, Johnny, Smith,man, 33, 22 2nd Street, New York, NY, 10021
The Best I could do is the foillowing but it does not come close... Your help is much apreciated
jq --raw-output 'to_entries | map_values({ job: .key } + .value )'
jq -r 'keys[] as $k | [ $k ] + [ .[$k] | getpath(paths(scalars)) ] | @csv' file