cd /news/developer-tools/jq-tips · home topics developer-tools article
[ARTICLE · art-9744] src=gist.github.com ↗ pub= topic=developer-tools verified=true sentiment=· neutral

jq Tips

This article provides a curated list of practical tips for using **jq**, a command-line JSON processor. The tips cover common tasks such as extracting key names, prettifying JSON, counting array elements, and retrieving specific values from nested arrays and objects. Advanced examples demonstrate how to format results as arrays, filter for unique values, and concatenate extracted data into a CSV file.

read1 min views23 publishedNov 21, 2018

Below are some curated jq tips:

  • Get key names from JSON using jq jq 'keys' file.json
  • Prettify a JSON file cat input-file.json | jq > output-file.json
  • Count array elements within a string echo '[{"name": "Sonia"}, {"name": "Dave"}]' | jq '. | length'
  • Count array elements within a file jq '. | length' input-file.json If the JSON records contain the same number of elements, you can simply print the first output line as follows: jq '. | length' input-file.json | head -1
  • Count elements in nested arrays echo '{"users":[{"name": "Sonia"}, {"name": "Dave"}]}' | jq '.users | length'
  • Get values within an array of objects Assuming you have an array of objects defined as follows:
{
"teis": [
{
"created": "2022-07-28T13:36:27.981",
"modifiedBy": "admin",
"name": "Jasmine",
"value": "[-11.4569446816544,8.08529544735806]",
"psi": {
"id": "aa93b509271"
},
"de": {
"id": "F3ogKBuviRA"
}
},
{
"created": "2022-07-28T13:36:27.981",
"modifiedBy": "admin",
"name": "Jordan",
"value": "54",
"psi": {
"id": "aa93b509271"
},
"de": {
"id": "qrur9Dvnyt5"
}
}
]
}

To get all the id s of the psi property:

cat input-file.json | jq '.teis[].psi.id'
jq '.teis[].psi.id' input-file.json

To get the result as an array, rather than a list of strings: jq -s 'map(.teis[].psi.id)' input-file.json To get only unique values returned in the array: jq -s 'map(.teis[].psi.id) | unique' input-file.json Count the number of unique items returned: jq -s 'map(.teis[].psi.id) | unique | length' input-file.json Concatenate extracted values as CSV: cat input-file.json | jq -r '.teis[].psi.id' | paste -sd "," > output.csv

── more in #developer-tools 4 stories · sorted by recency
── more on @jq 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/jq-tips] indexed:0 read:1min 2018-11-21 ·