jq playground
Work out a jq filter on your own JSON before it goes into a script. The filter runs in your browser, so the JSON you paste isn’t uploaded.
JSON
Result
In a terminal
jq '.' file.jsonCommon jq filters
.robots[].name.[]outputs the items of an array one at a time, and.namereads that key from each one."Tidy" "Dusty" "Crumbs" "Captain Lint" "Socks".robots[] | select(.dust_g > 90) | .nameselect(…)passes on only the items where the condition is true."Crumbs" "Captain Lint".robots[] | {id, room: .location.room}Braces build a new object.
{id}is short for{id: .id}.{"id":"TIDY-001","room":"lobby"} {"id":"TIDY-002","room":"open plan"} {"id":"TIDY-003","room":"kitchen"} {"id":"TIDY-004","room":"meeting room 2"} {"id":"TIDY-005","room":"open plan"}.robots | lengthCounts the items in an array, the keys of an object, or the characters in a string.
5.robots[0] | keysLists an object’s keys in alphabetical order.
keys_unsortedkeeps the order they were written in.["dust_g","id","location","name","status"].robots | sort_by(.dust_g) | map(.name)Sorts an array by one value, lowest first. Add
| reverseaftersort_by(…)for highest first.["Dusty","Tidy","Socks","Captain Lint","Crumbs"].robots | group_by(.status) | map({status: .[0].status, count: length})group_bysplits an array into one array per value, and themapafter it turns each group into a count.[{"status":"cleaning","count":2},{"status":"docked","count":2},{"status":"stuck","count":1}].robots | map(.dust_g) | addAdds up an array of numbers. It also joins strings and merges objects.
522.robots[1].location | to_entriesTurns an object into a list of key and value pairs.
from_entriesturns the list back into an object.[{"key":"floor","value":2},{"key":"room","value":"open plan"}].robots[] | .stuck_under // "not stuck"Uses the value on the right when the one on the left is missing,
nullorfalse."not stuck" "standing desk" "not stuck" "not stuck" "not stuck".robots[] | if .dust_g > 90 then "\(.name): empty the bin" else empty endemptyoutputs nothing, so items that fail the test are dropped.\(.name)inside a string inserts a value."Crumbs: empty the bin" "Captain Lint: empty the bin".robots[0] | del(.location, .dust_g)Removes keys from an object. Separate several paths with commas.
{"id":"TIDY-001","name":"Tidy","status":"docked"}.robots[] | select(.status == "stuck") | .status |= ascii_upcaseChanges a value in place and outputs the whole object.
|=runs the filter on its right against the current value.{"id":"TIDY-002","name":"Dusty","status":"STUCK","location":{"floor":2,"room":"open plan"},"dust_g":18,"stuck_under":"standing desk"}.robots[] | [.id, .name, .dust_g] | @csvTurns an array into a CSV row. Use it with
-r, or each row prints as a JSON string with its quotes escaped."TIDY-001","Tidy",42 "TIDY-002","Dusty",18 "TIDY-003","Crumbs",310 "TIDY-004","Captain Lint",95 "TIDY-005","Socks",57
Running a filter in a terminal
jq takes the filter first, then a file name. Without a file name it reads standard input, so you can pipe curl into it.
# A file
jq '.robots[].name' robots.json
# An API response
curl -s https://api.crumbpatrol.test/robots | jq '.robots | length'
# CSV, written to a file
jq -r '.robots[] | [.id, .name, .dust_g] | @csv' robots.json > robots.csv
# JSON Lines: count the lines where event is "stuck"
jq -s 'map(select(.event == "stuck")) | length' events.jsonlSingle quotes around the filter stop the shell from interpreting $, | and the double quotes inside it. Windows Command Prompt has no single quotes, so use double quotes outside and \" inside:
jq ".robots[] | select(.status == \"stuck\")" robots.json-c prints each result on one line. -s reads every value in the input into one array before the filter runs, for a filter that needs all the lines of a JSON Lines file at once.
To install jq, run brew install jq, apt install jq or winget install jqlang.jq. The jq manual documents every function.
What’s supported
The playground runs a JavaScript version of jq written for this site, which covers part of the language. Every example above is tested to print the same output as jq 1.7.1.
- Paths
.key."a key".[0].[-1].[2:5].[]...key?- Combining
|,//( )[ ]{ }"text \(.key)"# comment- Operators
==!=<<=>>=andor+-*/%- Assignment
=|=+=-=*=/=%=//=- Conditions
if … then … elif … else … end- Functions
addallanyarraysascii_downcaseascii_upcasebooleanscaptureceilcontainsdelemptyendswithfirstflattenfloorfrom_entriesfromdatefromjsongroup_bygsubhasinsideiterablesjoinkeyskeys_unsortedlastlengthlimitltrimstrmapmap_valuesmaxmax_byminmin_bynotnownullsnumbersobjectspathpathsrangerecursereverseroundrtrimstrscalarsscanselectsortsort_bysplitsqrtstartswithstringssubtestto_entriestodatetojsontonumbertostringtypeuniqueunique_byvalueswalkwith_entries- Formats
@text@json@csv@tsv@base64@base64d@uri- Input
- One JSON document, or JSON Lines with one value per line. Files up to 2 MB.
Not supported
- Variables:
as $name,--argand$ENV. reduce,foreachanddef.tryandcatch. The?operator works.getpath,setpath,input,env,match, and the date functions apart fromtodate,fromdateandnow.@sh,@htmland a format followed by a string, such as@csv "\(.row)".- Slices inside
del()or on the left of an assignment, as indel(.[1:]).
Differences from jq
- Numbers are JavaScript numbers. An integer above 9007199254740992 is rounded, and
1.0prints as1. jq 1.7 prints both as written when the filter doesn’t change them. - Object keys that are whole numbers, such as
"10", print before the other keys. - Regular expressions follow JavaScript’s rules where jq uses Oniguruma’s. Groups, named captures like
(?<id>\d+), character classes and quantifiers are written the same way in both. Only thegandiflags work. - Errors raised while a filter runs use jq 1.7’s wording, such as Cannot index string with string. Syntax errors are worded differently.
A share link holds the JSON and the filter in the part of the address after #, which browsers don’t send to a server. Anyone you give the link to can read both.