JSON file example.
{
"entities": [
{
"id": "d130",
"name": "Abdoulaye, Aminata",
"division": {
"id": "d130",
"name": "",
"selfUri": "/api/v2/authorization/"
},
"chat": {
"jabberId": "62a0e0b060"
},
"email": "Aminata.Abdoulaye@",
"primaryContactInfo": [
{
"address": "Aminata.Abdoulaye@",
"mediaType": "EMAIL",
"type": "PRIMARY"
}
],
"addresses": [
{
"address": "Aminata.Abdoulaye@",
"mediaType": "EMAIL",
"type": "WORK"
}
],
When converting JSON to csv, I have managed to create a csv file containing the data but there is no headers being displayed in the csv file.
gc --profile NewProfile users list -a --pageSize 100 | jq-win64 -r -c ".[1,2,3] | [.username,.addresses[].address,.id,.title]| @csv" > test.csv
I have managed to create a csv file that contains the headers but no data.
gc --profile NewProfile users list -a --pageSize 100 | jq-win64 -r ".? |(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv" > test.csv
Then when i try to merge the two together, I get an error saying, string ("Waled.Abdu...) has no keys
gc --profile NewProfile users list -a --pageSize 100 | jq-win64 -r ".[1,2,3] | [.username,.addresses[].address,.id,.title] |(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv" > test.csv
jq: error (at <stdin>:39688): string ("Waled.Abdu...) has no keys
Waled's details in JSON are shown below.
{
"id": "b143",
"name": "Abdulkadir, Waled",
"division": {
"id": "b143",
"name": "",
"selfUri": "/api/v2/authorization/"
},
"chat": {
"jabberId": "63e6e"
},
"department": "Canada Solutions",
"email": "Waled.Abdulkadir@",
"primaryContactInfo": [
{
"address": "Waled.Abdulkadir@",
"mediaType": "EMAIL",
"type": "PRIMARY"
}
],
"addresses": [],
"state": "active",
"title": "Engineer",
"username": "Waled.Abdulkadir@",
"manager": {
"id": "f3cc8f83-14c0",
"selfUri": "/api/v2/users/14c0"
},
"version": 4,
"acdAutoAnswer": false,
"selfUri": "/api/v2/users/14c0"
},
Any suggestions would be greatly appreciated. Thanks
I'd go with something like:
jq -r '["username", "address", "id", "title"],
(.entities[]
| (try .addresses[].address // null) as $a
| [.username, $a, .id, .title] )
| @csv'
To circumvent shell quoting issues, consider using the -f command-line option.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.