简体   繁体   English

使用jq解析JSON文件时where子句问题

[英]Where clause issue when parsing JSON file using jq

I'm trying to parse a JSON file which has 6 million lines. 我正在尝试解析具有600万行的JSON文件。 Which something looks like this: 看起来像这样:

temp.json temp.json

{
  "bbc.com": {
    "Reputation": "2.1",
    "Rank": "448",
    "Category": [
      "News"
    ]
  },
  "amazon.com": {
    "Reputation": "2.1",
    "Rank": "448",
    "Category": [
      "Shopping"
    ]
  }
}

I know how to parse the "Keys" alone. 我知道如何单独解析“键”。 To get "Keys" of this JSON structure, I tried, 为了获得此JSON结构的“键”,我尝试过,

jq -r 'keys[]' temp.json 

Result : 结果:

amazon.com
bbc.com

To get the "Category" in the above JSON file . 要获取上述JSON文件中的“类别”。 I tried , 我试过了 ,

jq -r '.[].Category[]' temp.json 

Result : 结果:

Shopping
News

How to get the "Keys" where the "Category" only with "Shopping"? 如何在“类别”仅与“购物”一起获得“钥匙”?

使用to_entries函数,如下所示:

jq -r 'to_entries[] | select(.value.Category | index("Shopping") != null) | .key'

在这种特殊情况下, to_entries及其开销,同时仍然可以提供简洁明了的解决方案:

keys[] as $k | select( .[$k].Category | index("Shopping") != null) | $k

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

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