Arun
Arun

Reputation: 1220

Where clause issue when parsing JSON file using jq

I'm trying to parse a JSON file which has 6 million lines. Which something looks like this:

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,

jq -r 'keys[]' temp.json 

Result :

amazon.com
bbc.com

To get the "Category" in the above JSON file . I tried ,

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

Result :

Shopping
News

How to get the "Keys" where the "Category" only with "Shopping"?

Upvotes: 1

Views: 416

Answers (2)

peak
peak

Reputation: 116750

In this particular case, to_entries and its overhead can be avoided while still yielding a concise and clear solution:

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

Upvotes: 5

Hans Z.
Hans Z.

Reputation: 53948

Use the to_entries function as in:

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

Upvotes: 5

Related Questions