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
65
votes
3 answers

jq - How do I print a parent value of an object when I am already deep into the object's children?

Say I have the following JSON, stored in my variable jsonVariable. { "id": 1, "details": { "username": "jamesbrown", "name": "James Brown" } } I parse this JSON with jq using the following: echo $jsonVariable | jq…
x3nr0s
  • 1,946
  • 4
  • 26
  • 46
65
votes
5 answers

passing arguments to jq filter

Here is my config.json: { "env": "dev", "dev": { "projects" : { "prj1": { "dependencies": {}, "description": "" } } } } Here are my bash…
lisi4ok
  • 725
  • 1
  • 6
  • 9
65
votes
3 answers

Exclude column from jq json output

I would like to get rid of the timestamp field here using jq JSON processor. [ { "timestamp": 1448369447295, "group": "employees", "uid": "elgalu" }, { "timestamp": 1448369447296, "group": "employees", "uid": "mike" …
Leo Gallucci
  • 16,355
  • 12
  • 77
  • 110
64
votes
1 answer

getting all the values of an array with jq

I parse a json file with jq: jq .response[1].text file.json It works fine, but each time I have to enter the number .response[2].text, .response[3].text etc. I want to get all the values at once (200 values) But when I do: jq .response[].text…
Miss Alena
  • 645
  • 1
  • 5
  • 5
61
votes
2 answers

How to convert a string to an integer in a JSON file using jq?

I use jq to transform a complex json object into a tinier one. My query is: jq 'to_entries[]| {companyId: (.key), companyTitle: (.value.title), companyCode: (.value.booking_service_code)}' companies.json Now, the (.key) is parsed as a string, yet I…
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
61
votes
6 answers

Deleting multiple keys at once with jq

I need to delete multiple keys at once from some JSON (using jq), and I'm trying to learn if there is a better way of doing this, than calling map and del every time. Here's my input data: test.json [ { "label": "US : USA : English", …
antun
  • 2,038
  • 2
  • 22
  • 34
60
votes
6 answers

How to check if element exists in array with jq

I have an array and I need to check if elements exists in that array or to get that element from the array using jq, fruit.json: { "fruit": [ "apple", "orange", "pomegranate", "apricot", "mango" …
idmitriev
  • 4,619
  • 4
  • 28
  • 44
58
votes
3 answers

Using jq to fetch key value from json output

I have a file that looks as below: { "repositories": [ { "id": "156c48fc-f208-43e8-a631-4d12deb89fa4", "namespace": "rhel12", "namespaceType": "organization", "name": "rhel6.6", "shortDescription": "", "visibility":…
meallhour
  • 13,921
  • 21
  • 60
  • 117
57
votes
2 answers

jq dates and unix timestamps

So I have a data with bunch of unix timestamp values (in milliseconds). Something like this: { "id": "f6922fd5-4f97-4113-820e-b45eba0ae236", "published_at": 1461624333859, "tracking_id": "a85d5ed5-5efa-461b-aae0-beb2098c0ff7", }, { …
iLemming
  • 34,477
  • 60
  • 195
  • 309
56
votes
8 answers

Exporting JSON to environment variables

If I have a JSON like this, { "hello1": "world1", "testk": "testv" } And I want to export each of these key-value pairs as environment variables, how to do it via shell script? So for example, when I write on the terminal, echo $hello1,…
Qirohchan
  • 1,057
  • 1
  • 9
  • 15
56
votes
1 answer

How to extract a field from each object in an array with jq?

I performed a cURL request to get all the users in our Gitlab server, like this. $ curl -H "Private-Token: a;sldkfja;slkdfj" https://gitlab.domain.com/api/v3/users?active=true > my_file.json and (part of) my_file.json looks like: [ { …
Chris F
  • 14,337
  • 30
  • 94
  • 192
56
votes
13 answers

Converting CSV to JSON in bash

Trying to convert a CSV file into a JSON Here is two sample lines : -21.3214077;55.4851413;Ruizia cordata -21.3213078;55.4849803;Cossinia pinnata I would like to get something like : "occurrences": [ { …
HydrUra
  • 1,336
  • 2
  • 13
  • 23
55
votes
3 answers

How to use jq when the variable has reserved characters?

I'm trying to get the following to work and it's not, help me please: curl -s 'https://cryptofresh.com/api/asset/markets?asset=MKR' | jq .OPEN.BTC The variable in question includes a period, I tried just about everything to escape the period &&…
Jonny Gerold
  • 551
  • 1
  • 4
  • 3
54
votes
4 answers

Check if string is a valid JSON with jq

I need to catch an error when lifting a service. The response can be null, a string error message like error services-migration/foobar: Not found: services-migration/foobar or a valid JSON when everything is fine. I was wondering if there is a way…
Milkncookiez
  • 6,817
  • 10
  • 57
  • 96
53
votes
5 answers

List public IP addresses of EC2 instances

I want to list the public IP addresses of my EC2 instances using Bash, separated by a delimiter (space or a new-line). I tried to pipe the output to jq with aws ec2 describe-instances | jq, but can't seem to isolate just the IP addresses. Can this…
Bas Peeters
  • 3,269
  • 4
  • 33
  • 49