简体   繁体   English

使用 jq 解析 JSON 输出

[英]JSON parsing output using jq

I am parsing the output from the below curl as follows -我正在解析以下 curl 的输出,如下所示 -

 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.我真的很感激这里的任何帮助。

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).如果您实际上一次只需要输出中的一系列 JSON 片段中的字典,则取出外部[...] (可能与-c结合使用,然后每行获取一个字典)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM