Questions tagged [jq]

Questions about the command line JSON processing tool jq.

jq is a command-line JSON processor for slicing, filtering, mapping and transforming structured JSON data.

Further reading

366 questions
138
votes
1 answer

How to prettyprint json using jq standalone?

Currently, when I want to pretty-print a json file using jq, I use: cat file.json | jq . Yet I would expect for jq to accept a file name as argument without having to fallback on cat. The man page says: jq [options...] filter [files...] ... By…
k0pernikus
  • 14,853
  • 21
  • 58
  • 79
95
votes
8 answers

Using jq to extract values from column-oriented JSON and format in CSV

I have the below JSON file, with the data stored as columns enumerated by rank in an array: { "data": [ { "displayName": "First Name", "rank": 1, "value": "VALUE" }, { "displayName": "Last Name", "rank":…
Kerim
  • 951
  • 1
  • 6
  • 3
85
votes
1 answer

jq - select an attribute beginning with a string

input json: [ { "id": "89", "hostname": "abcd" }, { "id": "89", "hostname": "babcd" } ] How to modify below filter to get on output only hostname beginning with "abcd"? $ jq -r '.[]|select(.hostname | contains("abcd"))'…
Chris
  • 3,591
  • 7
  • 24
  • 35
68
votes
4 answers

Merge jq output into a comma separated string

I am trying to curl some URL which returns a json file, then I want to parse hosts from it and create a comma separated string. I have the first part working curl -s -u "admin:admin" -H "X-Requested-By: ambari"…
roy
  • 1,329
  • 2
  • 12
  • 13
60
votes
2 answers

How to convert embedded (quoted) json string to json

I'm familiar with "jq" for parsing json. I work with one service that produces a json response where one of the properties is itself a json string. How do I convert that quoted value to a valid json string so I can then process it with jq? For…
David M. Karr
  • 891
  • 1
  • 7
  • 16
47
votes
4 answers

Setting jq output to a Bash Variable

I'm using curl to get JSON back from a rest api like this: content=$(curl -s -X GET -H "Header:Value" http://127.0.0.1:8200/etc) echo "${content}"| jq -r '.data.value' which produces the value I need. However; when I change the above code to look…
jymbo
  • 955
  • 1
  • 7
  • 7
37
votes
4 answers

JSON array to bash variables using jq

I've got a JSON array like so: { "SITE_DATA": { "URL": "example.com", "AUTHOR": "John Doe", "CREATED": "10/22/2017" } } I'm looking to iterate over this array using jq so I can set the key of each item as the variable name and the…
EHerman
  • 551
  • 2
  • 5
  • 10
28
votes
2 answers

How to sort a stream of json objects by field value using jq

I'm starting with json that looks like this: { "object": "list", "data": [ { "id": "in_1HW85aFGUwFHXzvl8wJbW7V7", "object": "invoice", "account_country": "US", "customer_name": "clientOne", "date": 1601244686, …
alec
  • 1,508
  • 3
  • 15
  • 26
28
votes
2 answers

Using jq within pipe chain produces no output

The issue of jq needing an explicit filter when the output is redirected is discussed all over the web. But I'm unable to redirect output if jq is part of a pipe chain, even when an explicit filter is in use. Consider: touch in.txt tail -f in.txt |…
Heath Raftery
  • 1,232
  • 1
  • 15
  • 18
26
votes
2 answers

jq - print "-" for null values

input json: { "id": "3885", "login": "050111", "lastLoginTime": 1529730115000, "lastLoginFrom": "192.168.66.230" } { "id": "3898", "login": "050112", "lastLoginTime": null, "lastLoginFrom": null } I want to get output for login,…
Chris
  • 3,591
  • 7
  • 24
  • 35
22
votes
2 answers

jq - print values in one line

json input: [ { "name": "cust1", "grp": [ { "id": "46", "name": "BA2" }, { "id": "36", "name": "GA1" }, { "id": "47", "name": "NA1" }, { …
Chris
  • 3,591
  • 7
  • 24
  • 35
21
votes
5 answers

looping through JSON array in shell script

Below is the curl command output (file information about branch), need script or command to print file name, filetype and size. I have tried with jq but was able fetch single value ( jq '.values[].size') { "path": { "components": [], …
21
votes
4 answers

jq - add objects from file into json array

I want to add an array with elements and value into an existing json file using jq. I already have a file (input.json) with { "id": 9, "version": 0, "lastUpdTs": 1532371267968, "name": "Training" } I want to add this into another groups…
CLO
  • 215
  • 1
  • 2
  • 4
20
votes
2 answers

jq print key and value for all in sub-object

I found this Q/A with the solution to print all the keys in an object: jq -r 'keys[] as $k | "\($k), \(.[$k] | .ip)"' In my case I want to perform the above but on a sub-object: jq -r '.connections keys[] as $k | "\($k), \(.[$k] | .ip)"' What is…
Philip Kirkbride
  • 9,816
  • 25
  • 95
  • 167
19
votes
3 answers

How to find and replace multiple field values using jq?

In the following json file, { "email": "xxx", "pass": "yyy", "contact": [ { "id": 111, "name": "AAA" } ], "lname": "YYY", "name": "AAA", "group": [ { "name": "AAA", "lname": "YYY", } ], I…
user2181698
  • 193
  • 1
  • 2
  • 6
1
2 3
24 25