JQ

jq is a lightweight and flexible command-line JSON processor. https://jqlang.github.io/jq/download/ VSCode Extension https://marketplace.visualstudio.com/items?itemName=davidnussio.vscode-jq-playground

Download the sample JSON from

https://github.com/gchandra10/jqtutorial

Note: This JSON file has no root element. '.' is used.

1. View JSON file in readable format

$ jq '.' sample_nows.json

2. Read the First JSON element / object

$ jq 'first(.[])' sample_nows.json

3. Read the Last JSON element

$ jq 'last(.[])' sample_nows.json

4. Read top 3 JSON elements

$ jq 'limit(3;.[])' sample_nows.json

5. Read 2nd & 3rd element. Remember, Python has the same format. LEFT Side inclusive, RIGHT Side exclusive

$ jq '.[2:4]' sample_nows.json

6. Extract individual values. | Pipeline the output

$ jq '.[] | [.balance,.age]' sample_nows.json

7. Extract individual values and do some calculations

$ jq '.[] | [.age, 65 - .age]' sample_nows.json

8. Return CSV from JSON

$ jq '.[] | [.company, .phone, .address] | @csv ' sample_nows.json

9. Return Tab Separated Values (TSV) from JSON

$ jq '.[] | [.company, .phone, .address] | @tsv ' sample_nows.json

10. Return with custom pipeline delimiter ( | )

$ jq '.[] | [.company, .phone, .address] | join("|")' sample_nows.json

Pro TIP : Export this result > output.txt and Import to db using bulk import tools like bcp, load data infile

11. Convert the number to string and return | delimited result

$ jq '.[] | [.balance,(.age | tostring)] | join("|") ' sample_nows.json

12. Process Array return Name (returns as list / array)

$ jq '.[] | [.friends[].name]' sample_nows.json

or (returns line by line)

$ jq '.friends[].name' sample_nows.json

13. Parse multi level values

$ jq '.[] | [.name.first, .name.last]' sample_nows.json 
(returns as list / array)

$ jq '.[].name.first, .[].name.last' sample_nows.json 
(returns line by line )

14. Query values based on condition, say .index > 2

$ jq 'map(select(.index > 2))' sample_nows.json

$ jq 'map(select(.index > 2)) | .[] | [.index,.balance,.age]' sample_nows.json

15. Sorting Elements

# Sort by Age ASC
$ jq 'sort_by(.age)' sample_nows.json

# Sort by Age DESC
$ jq 'sort_by(-.age)' sample_nows.json

# Sort on multiple keys
$ jq 'sort_by(.age, .index)' sample_nows.json

Use Cases

curl -s https://www.githubstatus.com/api/v2/status.json
curl -s https://www.githubstatus.com/api/v2/status.json | jq '.'
curl -s https://www.githubstatus.com/api/v2/status.json | jq '.status'

Last updated