Home > Software engineering >  Using jq with a large JSON file
Using jq with a large JSON file

Time:12-20

I have an extremely large JSON file that I am working with, on my Linux box. When I jq the file I get an output in this format, which is perfect:

{ “ID:” 12345 “Name:” joe “Address:” 123 first street “Email:” [email protected]

My goal is to be able to grep for a particular field but get all related fields to return. So if I did a grep for “123 first street” I would also get the ID , name, and email that was with that group of data.

Thus far, I have gotten here:

jq . Myfile.json | grep “123 first street”

Can anyone help with me with getting this query right? I would like to stay with this JSON format and stay in the Linux box.

jq . Myfile.json | grep “123 first street”

CodePudding user response:

You can use the --raw-output option for grep to print the whole line that contains the search string. This will allow you to see the whole JSON object that contains the "Address" field with the value "123 first street". Here's an example:

jq . Myfile.json | grep --raw-output "123 first street"

Alternatively, you can use the --color=never option for grep to suppress the color output, which can make the output easier to read. Here's an example:

jq . Myfile.json | grep --color=never "123 first street"

If you want to extract specific fields from the JSON object that contains the "Address" field with the value "123 first street", you can use jq to filter the output. For example, the following command will extract the "ID", "Name", and "Email" fields:

jq '. | select(.Address == "123 first street") | {ID, Name, Email}' Myfile.json

This will output a JSON object with the selected fields, like this:

{
  "ID": 12345,
  "Name": "joe",
  "Email": "[email protected]"
}

You can also use jq to extract specific fields and format the output as a string. For example, the following command will extract the "ID", "Name", and "Email" fields and format them as a string with a comma-separated list:

jq -r '. | select(.Address == "123 first street") | [.ID, .Name, .Email] | @csv' Myfile.json

This will output a string like this:

"12345","joe","[email protected]"

CodePudding user response:

This should return all JSON objects with "field".

jq '.[] | select(has("field"))'
  •  Tags:  
  • json
  • Related