Search code examples
jsoncsvexport-to-csvjq

jq - json list of dicts to csv


Input JSON example:

[
  {"name":"Θεμιστοκλής","surname":"Παπαϊωάννου","gender":"male","region":"Greece"}, 
  {"name":"Casian","surname":"Cusin","gender":"male","region":"Romania"}
]

Now with my code I have output as strings:

$ curl -s https://uinames.com/api/?amount=3 | jq '.[] | [.name, .surname] | @csv'
"\"Anamaria\",\"Tămaș\""
"\"Aurora\",\"Coronado\""
"\"Εύηνος\",\"Ελευθερόπουλος\""

What I need:

"Anamaria","Tămaș"
"Aurora","Coronado"
"Εύηνος","Ελευθερόπουλος"

Can anybody explain please what I do wrong?


Solution

  • You should add the -r command-line option when invoking jq.

    Example:

    $ curl -Ss 'https://uinames.com/api/?amount=3' |
       jq -r '.[] | [.name, .surname] | @csv' 
    "Γόργασος","Θεοδωρίδης"
    "Marta","Brediceanu"
    "Iulian","Bârcă"