Home > Net >  JSON parsing output using jq
JSON parsing output using jq

Time:06-03

I am parsing the output from the below curl as follows -

 curl 'http://www.bom.gov.au/fwo/IDN60801/IDN60801.95765.json' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36' | jq '.[][][] | select(.apparent_t>5)

How can retain only certain fields in the output. Expected output is

[
   {
      "sort_order":147,
      "lat":-33.8,
      "lon":151.1,
      "apparent_t":10.9
   },
   {
      "sort_order":148,
      "lat":-23.8,
      "lon":128.1,
      "apparent_t":7
   },
   {
      "sort_order":236,
      "lat":-33.34,
      "lon":151.74,
      "apparent_t":21.9
   }
]

Basically, I need to filter out only certain fields & print key, values for it. I am not able to work out th next steps for this. I'd truly appreciate any help here.

CodePudding user response:

If you know the keys you want to extract, just ... extract those.

curl 'http://www.bom.gov.au/fwo/IDN60801/IDN60801.95765.json' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36' |
jq '[.[][][] | select(.apparent_t>5) | {"sort_order": .sort_order, "lat": .lat, "lon": .lon, "apparent_t": .apparent_t}]'

This outputs a list of dictionaries; take out the outer [...] if you actually want just a dictionary at a time in a sequence of JSON fragments in the output (maybe combine with -c then to get one dictionary per line).

  •  Tags:  
  • json
  • Related