{"slug": "jq-tips", "title": "jq Tips", "summary": "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.", "body_md": "Below are some curated jq\ntips:\n- Get key names from JSON using jq\njq 'keys' file.json\n- Prettify a JSON file\ncat input-file.json | jq > output-file.json\n- Count array elements within a string\necho '[{\"name\": \"Sonia\"}, {\"name\": \"Dave\"}]' | jq '. | length'\n- Count array elements within a file\njq '. | length' input-file.json\nIf the JSON records contain the same number of elements, you can simply print the first output line as follows:\njq '. | length' input-file.json | head -1\n- Count elements in nested arrays\necho '{\"users\":[{\"name\": \"Sonia\"}, {\"name\": \"Dave\"}]}' | jq '.users | length'\n- Get values within an array of objects\nAssuming you have an array of objects defined as follows:\n{\n\"teis\": [\n{\n\"created\": \"2022-07-28T13:36:27.981\",\n\"modifiedBy\": \"admin\",\n\"name\": \"Jasmine\",\n\"value\": \"[-11.4569446816544,8.08529544735806]\",\n\"psi\": {\n\"id\": \"aa93b509271\"\n},\n\"de\": {\n\"id\": \"F3ogKBuviRA\"\n}\n},\n{\n\"created\": \"2022-07-28T13:36:27.981\",\n\"modifiedBy\": \"admin\",\n\"name\": \"Jordan\",\n\"value\": \"54\",\n\"psi\": {\n\"id\": \"aa93b509271\"\n},\n\"de\": {\n\"id\": \"qrur9Dvnyt5\"\n}\n}\n]\n}\nTo get all the id\ns of the psi\nproperty:\ncat input-file.json | jq '.teis[].psi.id'\n# Or\njq '.teis[].psi.id' input-file.json\nTo get the result as an array, rather than a list of strings:\njq -s 'map(.teis[].psi.id)' input-file.json\nTo get only unique values returned in the array:\njq -s 'map(.teis[].psi.id) | unique' input-file.json\nCount the number of unique items returned:\njq -s 'map(.teis[].psi.id) | unique | length' input-file.json\nConcatenate extracted values as CSV:\ncat input-file.json | jq -r '.teis[].psi.id' | paste -sd \",\" > output.csv", "url": "https://wpnews.pro/news/jq-tips", "canonical_source": "https://gist.github.com/barbietunnie/e694d8a8b3f5fd895829e44e7b2c42eb", "published_at": "2018-11-21 13:43:59+00:00", "updated_at": "2026-05-22 18:37:26.264846+00:00", "lang": "en", "topics": ["developer-tools", "data"], "entities": ["jq"], "alternates": {"html": "https://wpnews.pro/news/jq-tips", "markdown": "https://wpnews.pro/news/jq-tips.md", "text": "https://wpnews.pro/news/jq-tips.txt", "jsonld": "https://wpnews.pro/news/jq-tips.jsonld"}}