Questions tagged [jq]

jq is a sed-like tool for JSON data – you can use it to slice, filter, map, and transform structured data with the same ease that sed, awk, grep and friends lets you play with text.

jq is a succinct programming language for querying and manipulating JSON data or plain text files. You can use it to slice, filter, map, and transform structured data with the same ease that sed, awk, grep and friends let you play with textual data. jq is stream-oriented and uses the pipe symbol | to connect filters familiarly.

Resources

6401 questions
102
votes
1 answer

jq: output array of json objects

Say I have the input: { "name": "John", "email": "john@company.com" } { "name": "Brad", "email": "brad@company.com" } How do I get the output: [ { "name": "John", "email": "john@company.com" }, { …
Mauricio Trajano
  • 2,677
  • 2
  • 20
  • 25
100
votes
6 answers

How to check for presence of 'key' in jq before iterating over the values

I get Cannot iterate over null (null) from the below query because .property_history is not present in result object. How do i check for the presence of .property_history key before proceeding with map(...) ? I tried using something like sold_year=…
Rahul Dess
  • 2,397
  • 2
  • 21
  • 42
97
votes
5 answers

get the first (or n'th) element in a jq json parsing

I can get the 1st element in a json inside [] $ echo '[{"a":"x", "b":true}, {"a":"XML", "b":false}]' | jq '.[1]' { "a": "XML", "b": false } But if the json is already disassembled (for instance, after filtering entries using 'select'), how can…
Demian Glait
  • 973
  • 1
  • 6
  • 4
96
votes
5 answers

Install jq JSON processor on Ubuntu 10.04

Is there a way to install jq JSON processor on Ubuntu 10.04? I Tried the usual sudo apt-get install jq but got the error E: Couldn't find package jq
Ste-3PO
  • 1,165
  • 1
  • 9
  • 18
95
votes
8 answers

Can I pass a string variable to jq not the file?

I want to convert JSON string into an array in bash. The JSON string is passed to the bash script as an argument (it doesn't exist in a file). Is there a way of achieving it without using some temp files? Similarly to this: script.sh #!…
Maciek Rek
  • 1,525
  • 2
  • 14
  • 18
93
votes
4 answers

How to filter array of objects by element property values using jq?

I like to filter json files using jq: jq . some.json Given the json containing an array of objects: { "theList": [ { "id": 1, "name": "Horst" }, { "id": 2, "name": "Fritz" }, { "id": 3, …
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
92
votes
11 answers

Iterating through JSON array in Shell script

I have a JSON data as follows in data.json file [ {"original_name":"pdf_convert","changed_name":"pdf_convert_1"}, {"original_name":"video_encode","changed_name":"video_encode_1"}, …
kosta
  • 4,302
  • 10
  • 50
  • 104
91
votes
4 answers

How to combine the sequence of objects in jq into one object?

I would like to convert the stream of objects: { "a": "green", "b": "white" } { "a": "red", "c": "purple" } into one object: { "a": "red", "b": "white", "c": "purple" } Also, how can I wrap the same sequence into an array? [ { …
Jennifer M.
  • 1,398
  • 1
  • 9
  • 11
85
votes
2 answers

Convert string to json in jq

Background I have a json file that contains a string of json within an object: { "requestType": "POST", "response": { "size": 78, "text": "{\"recordID\":123, \"title\":\"Hello World\", \"content\":\"Lorem ipsum...\"}" …
RJ-Adam
  • 969
  • 1
  • 6
  • 9
82
votes
5 answers

Output specific key value in object for each element in array with jq for JSON

I have an array: [ { "AssetId": 14462955, "Name": "Cultural Item" }, { "AssetId": 114385498, "Name": "Redspybot" }, { "AssetId": 29715011, "Name": "American Cowboy" }, { …
Zimbabwe Elephant
  • 965
  • 1
  • 8
  • 12
74
votes
4 answers

jq - How to filter a json that does not contain

I have an aws query that I want to filter in jq. I want to filter all the imageTags that don't end with "latest" So far I did this but it filters things containing "latest" while I want to filter things not containing "latest" (or not ending with…
Gavriel Fishel
  • 865
  • 1
  • 6
  • 10
74
votes
2 answers

jq not working on tag name with dashes and numbers

I am using jq but having "-" in my json tag make jq not compile. I cannot escape it to make it works. Here is the command: curl -X GET -H "X-AppKey:foo" "foo/v2/_status" | jq '.component-status[]' I have read in the github of jq this post…
paul
  • 12,873
  • 23
  • 91
  • 153
66
votes
3 answers

jq select value from array

I have the following JSON file with example values: { "files": [{ "fileName": "FOO", "md5": "blablabla" }, { "fileName": "BAR", "md5": "alaldlafj" }] } Now what I want is to return the md5 value where for…
Marvin Effing
  • 2,693
  • 3
  • 20
  • 35
66
votes
6 answers

How to convert a JSON object to key=value format in jq?

In jq, how can I convert a JSON to a string with key=value? From: { "var": 1, "foo": "bar", "x": "test" } To: var=1 foo=bar x=test
gianebao
  • 17,718
  • 3
  • 31
  • 40
65
votes
5 answers

Using jq with bash to run command for each object in array

How can I run a Bash command for every JSON object in a JSON array using jq? So far I have this: cat credentials.json | jq -r '.[] | .user, .date, .email' | mycommand -u {user} -d {date} -e {email} This doesn't seem to work. How can I take the…
danielrvt
  • 10,177
  • 20
  • 80
  • 121