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
174
votes
9 answers

Using jq or alternative command line tools to compare JSON files

Are there any command line utilities that can be used to find if two JSON files are identical with invariance to within-dictionary-key and within-list-element ordering? Could this be done with jq or some other equivalent tool? Examples: These two…
Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564
171
votes
3 answers

how to parse a JSON String with jq (or other alternatives)?

I'm trying to get jq to parse a JSON structure like: { "a" : 1, "b" : 2, "c" : "{\"id\":\"9ee ...\",\"parent\":\"abc...\"}\n" } That is, an element in the JSON is a string with escaped json. So, I have something along the lines of $ jq [.c]…
Colin Grogan
  • 2,462
  • 3
  • 12
  • 12
151
votes
2 answers

jq: print key and value for each entry in an object

How do I get jq to take json like this: { "host1": { "ip": "10.1.2.3" }, "host2": { "ip": "10.1.2.2" }, "host3": { "ip": "10.1.18.1" } } and generate this output: host1, 10.1.2.3 host2, 10.1.2.2 host3, 10.1.18.1 I'm not interested in the…
Jeff Tang
  • 1,723
  • 3
  • 11
  • 7
150
votes
3 answers

Get outputs from jq on a single line

I got below output using: https://stackoverflow.com/a/40330344 (.issues[] | {key, status: .fields.status.name, assignee: .fields.assignee.emailAddress}) Output: { "key": "SEA-739", "status": "Open", "assignee": null } { "key":…
Jitesh Sojitra
  • 3,655
  • 7
  • 27
  • 46
149
votes
9 answers

Modify a key-value in a json using jq in-place

I have a json in which I want to modify a particular value but the terminal always displays the json with the modified value but it does not actually change the value in the particular file. Sample json: { "name": "abcd", "age": 30, …
wolfsbane
  • 1,729
  • 2
  • 11
  • 17
149
votes
2 answers

Concat 2 fields in JSON using jq

I am using jq to reformat my JSON. JSON String: {"channel": "youtube", "profile_type": "video", "member_key": "hello"} Wanted output: {"channel" : "profile_type.youtube"} My command: echo '{"channel": "youtube", "profile_type": "video",…
darthsidious
  • 2,851
  • 3
  • 19
  • 30
147
votes
8 answers

How to format a JSON string as a table using jq?

Just started out with Bash scripting and stumbled upon jq to work with JSON. I need to transform a JSON string like below to a table for output in the terminal. [{ "name": "George", "id": 12, "email": "george@domain.example" }, { …
Rein
  • 3,211
  • 3
  • 16
  • 21
143
votes
11 answers

jq to replace text directly on file (like sed -i)

I have a json file that needs to be updated on a certain condition. Sample json { "Actions" : [ { "value" : "1", "properties" : { "name" : "abc", "age" : "2", "other ": "test1" …
Supra
  • 1,453
  • 2
  • 10
  • 6
132
votes
3 answers

How to sort a json file by keys and values of those keys in jq

We're building a website using the Pentaho CTools library, which has a graphical dashboard editor which writes out JSON-format files for part of the dashboard. I'd like to apply a transform to these files before check-in to git in order to sort them…
karlos
  • 3,965
  • 3
  • 16
  • 19
124
votes
3 answers

Extract a specific field from JSON output using jq

I have a JSON output as follows: { "example": { "sub-example": [ { "name": "123-345", "tag" : 100 }, { "name": "234-456", "tag" : 100 }, { "name": "4a7-a07a5", "tag"…
rmb
  • 1,737
  • 4
  • 14
  • 20
116
votes
1 answer

jq Conditional output

I'm using jq to play with json. I was wondering how to conditionally print something in that. Say I am interested in a field call geo. I used the following command and find out there is only one entry whose geo is null: % cat all.json | jq '.geo !=…
clwen
  • 20,004
  • 31
  • 77
  • 94
113
votes
3 answers

Add new element to existing JSON array with jq

I want to append an element to an array in a JSON file using the jq``add command, but it's not working. report-2017-01-07.json file: { "report": "1.0", "data": { "date": "2010-01-07", "messages": [ { …
Felipe
  • 7,013
  • 8
  • 44
  • 102
105
votes
3 answers

How do I use jq to convert number to string?

Given the following jq command and Json: jq '.[]|[.string,.number]|join(": ")' <<< ' [ { "number": 3, "string": "threee" }, { "number": 7, "string": "seven" } ] ' I'm trying to format the output as: three: 3 seven:…
AXE Labs
  • 4,051
  • 4
  • 29
  • 29
104
votes
8 answers

How to run jq from gitbash in windows?

I have gitbash in Windows. I am trying to run jq but its giving me error. $ ./jq-win64.exe jq parse error: Invalid numeric literal at line 2, column 0 Intention: I want to use jq to parse json.
DotNetDeveloper
  • 1,051
  • 2
  • 7
  • 4
104
votes
1 answer

jq: Cannot index array with string

I have the following in a file (which I will call "myfile"): [{ "id": 123, "name": "John", "aux": [{ "abc": "random", "def": "I want this" }], "blah": 23.11 }] I could parse it if the file did not have the first…
Xu Wang
  • 10,199
  • 6
  • 44
  • 78