skip to Main Content

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

2

Answers


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

    throws an error in Ubuntu but not at jqplay.org

    Your Ubuntu machine is probably not using jq 1.7, which is the version currently running on jqplay.org. This version introduced a new syntax, which you are using with .glosses.[]. From the release info:

    Allow dot for chained value iterator .[], .[]? @wader #2650

    $ jq -n '{"a": [123]} | .a[]'
    123
    # now this also works
    $ jq -n '{"a": [123]} | .a.[]'
    123
    

    Thus, change your approach to use .glosses[] instead:

    jq '.lemma, .phonetic, .pos, .intransitive, .root.radicals, .glosses[].gloss'
    
    Login or Signup to reply.
  2. 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"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search