Often when working with JSON in a Linux/Unix environment, it is nice to be able to filter records based on the values of certain fields. jq is a lightweight command line JSON processor that is very easy to use. jq offers an easy way to filter JSON records based on field values with the select() function.

The jq 'select()' function evaluates expressions that return true or false. Records for which the expression returns true will be output.

Here is an example of filtering all JSON records where the “first_name” field equals “Amelia”.

$ cat bdPerson_v1_1k.json | jq 'select(.first_name == "Amelia")'

  "id": 1081405,
  "username": "agraves",
  "email_address": "[email protected]",
  "phone_number": "250-633-823",
  "first_name": "Amelia",
  "last_name": "Graves",
  "middle_name": "Kimberly",
  "sex": "FEMALE",
  "birthdate": "1976-01-31",
  "join_date": "2010-12-10T22:21:52.390-08:00",
  "previous_logins": 4463,
  "last_ip": ""

More examples of using jq can be found here

Leave a Reply

How to filter JSON records by value with jq