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
53
votes
5 answers

How to map an object to arrays so it can be converted to csv?

I'm trying to convert an object that looks like this: { "123" : "abc", "231" : "dbh", "452" : "xyz" } To csv that looks like this: "123","abc" "231","dbh" "452","xyz" I would prefer to use the command line tool jq but can't seem to figure…
lsl
  • 4,371
  • 3
  • 39
  • 54
52
votes
11 answers

How to convert a json response into yaml in bash

I read data from a json file with jq. I wanna append the results into a yaml file, but I dont get it working. I am quite new to shell programming. My goal is to append that "users" to an existing "users"-Array in a yaml file. This is my json…
Jan
  • 12,992
  • 9
  • 53
  • 89
50
votes
3 answers

Printing multiple values on the same line

I'm trying to parse a JSON document and print a couple of values on the same line. Is there a way to take the following document: { "fmep": { "foo": 112, "bar": 234324, "cat": 21343423 } } And spit out: 112 234324 I can get the…
Mickster
  • 643
  • 2
  • 9
  • 13
47
votes
1 answer

How do I keep colors when piping "jq" output to "less"?

I have a simple json file and if I pipe the output of "jq" into "less", the colors get removed. This works: # yey, lots of colors jq "." /tmp/myfile.json This doesn't work: # ugly output :( , no colors jq "." /tmp/myfile.json | less -R Any ideas…
filipg g
  • 483
  • 1
  • 4
  • 5
46
votes
5 answers

Using jq how can I replace the name of a key with something else

This should be easy enough... I want to rename a few keys (ideally with jq), whatever I do seems to error though. Here is a json example below: [ { "fruit": "strawberry", "veg": "apple", "worker": "gardener" } ] I'd like to rename the veg…
keeer
  • 783
  • 1
  • 5
  • 11
46
votes
3 answers

jq: group and key by property

I have a list of objects that look like this: [ { "ip": "1.1.1.1", "component": "name1" }, { "ip": "1.1.1.2", "component": "name1" }, { "ip": "1.1.1.3", "component": "name2" }, { "ip": "1.1.1.4", …
replay
  • 3,569
  • 3
  • 21
  • 30
46
votes
2 answers

JSON JQ if without else

I use the following JQ command to filter out the JSON. My requirement is to filter out the JSON message if the expected node is present. Or else, do nothing. Hence, I use if, elif, .... sed -n "s/.*Service - //p" $1/response.log* | jq "if…
user1578872
  • 7,808
  • 29
  • 108
  • 206
45
votes
2 answers

Using jq, convert array of objects to object with named keys

Given a json file in the format as : [ { name : "A", value : "1" }, { name : "B", value : "5" }, { name : "E", value : "8" } ] How would I convert it to something like this using jq: { "A" : { name : "A", value : "1" }, …
Mike N
  • 453
  • 1
  • 4
  • 4
44
votes
4 answers

Docker Error - "jq: error: Cannot iterate over null"

So I'm trying to deploy a dockerfile on Elastic Beanstalk, but I can't get past this error - "jq: error: Cannot iterate over null". Successfully built [myContainerId] Successfully built aws_beanstalk/staging-app [2015-01-29T10:35:59.494Z] INFO …
YYZ
  • 749
  • 4
  • 8
  • 18
43
votes
2 answers

How to filter an array of JSON objects with jq?

I have the following JSON input: { "zk_kafka": [ { "InstanceType": "t2.medium", "zkMemory": "16", "kafkaMemory": "8" }, { "InstanceType": "t2.small", "zkMemory": "8", "kafkaMemory": "4" } ], …
Rehan Ch
  • 805
  • 3
  • 13
  • 18
42
votes
3 answers

How to use jq to output JSONL (one independent JSON object per line)

My request sounds trivial but I could not find a way to do it. I have as input an array of JSON objects: [ { "foo": 1, "bar": 2 }, { "foo": 3, "bar": 4 }, (...) ] and I want as output the JSONL…
giacecco
  • 661
  • 1
  • 5
  • 8
42
votes
3 answers

How do I sum the values in an array of maps in jq?

Given a JSON stream of the following form: { "a": 10, "b": 11 } { "a": 20, "b": 21 } { "a": 30, "b": 31 } I would like to sum the values in each of the objects and output a single object, namely: { "a": 60, "b": 63 } I'm guessing this will…
Alan Burlison
  • 1,022
  • 1
  • 9
  • 16
39
votes
1 answer

How to combine an array into a single string value when using CSV output in jq?

I have the following jq command: cat myFile.json | jq -r '.tickets[] | [.created_at, .id, .via.channel, .tags[]] | @csv' And it outputs a line such as: "2016-02-02T10:00:00Z",99999,"web","tag1","tag2","tag3","tag4" I'm trying to join the .tags[]…
Emre Sevinç
  • 8,211
  • 14
  • 64
  • 105
39
votes
2 answers

get label value from docker inspect

I had problem to get the value from the map list due to the key has "." inside. docker inspect jenkins Config: { .. "Labels": { "com.docker.compose.config-hash":…
Larry Cai
  • 55,923
  • 34
  • 110
  • 156
39
votes
1 answer

Concat numbers from JSON without doublequotes using jq

I have files with 1 json document per row and the fields start_id and end_id in each document. I'd like to use jq to extract these and print them on the same row. So far I have: cat part* | jq '"\(.start_id) \(.end_id)"' | sed s/\"//g | head This…
Synesso
  • 37,610
  • 35
  • 136
  • 207