jsonsyntaxjqexport-to-csv

Convert .json to .csv


jq version jq-1.6 on Ubuntu 22.04.2 LTE

Looking to convert large .json file to .csv

json example:

{"_id":{"$oid":"5200a366e36f237975000783"},"derived_form":{"$numberInt":"1"},"intransitive":true,"lemma":"badbad","phonetic":"bɐdbɐt","pos":"VERB","root":{"radicals":"b-d-b-d"},"sources":["Spagnol2011","Falzon2013"],"glosses":[{"gloss":"to fornicate","examples":[]},{"gloss":"to cough a lot","examples":[]}],"norm_freq":{"$numberDouble":"0.0"}}

How do I convert to .csv:

5200a366e36f237975000783, badbad, bɐdbɐt, VERB, true, b-d-b-d, "to fornicate, to cough a lot"

jq '.lemma, .phonetic, .pos, .intransitive, .root.radicals, .glosses.[].gloss'

throws an error in Ubuntu but not at jqplay.org


Solution

  • To obtain the desired csv output:

    $ jq -r '"\(._id[]), \(.lemma), \(.phonetic), \(.pos), \(.intransitive), \(.root.radicals), \"\([.glosses[].gloss] | join(","))\""' file
    5200a366e36f237975000783, badbad, bɐdbɐt, VERB, true, b-d-b-d, "to fornicate,to cough a lot"