Skip to main content
191 votes
Accepted

jq - select an attribute beginning with a string

Solution: jq -r '.[]|select(.hostname | startswith("abcd"))' jjjj
Chris's user avatar
  • 4,751
127 votes

How to convert embedded (quoted) json string to json

With jq's fromjson function: Sample stuff.json contents: { "stuff": "{\"date\":\"2018-01-08\"}" } jq -c '.stuff | fromjson' stuff.json The output: {"date":"2018-01-08"}
RomanPerekhrest's user avatar
73 votes
Accepted

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

from man jq sort, sort_by(path_expression) The sort functions sorts its input, which must be an array. In general and invoking a separate jq command, you have to use -s, --slurp that will ...
thanasisp's user avatar
  • 8,532
59 votes
Accepted

jq - print "-" for null values

use the alternative operator: // so : $jq -r '.|[.login, .lastLoginTime // "-" , .lastLoginFrom // "-" ]|@tsv' test_json 050111 1529730115000 192.168.66.230 050112 - -
EchoMike444's user avatar
  • 3,225
48 votes
Accepted

jq print key and value for all in sub-object

Simply pipe to keys function: Sample input.json: { "connections": { "host1": { "ip": "10.1.2.3" }, "host2": { "ip": "10.1.2.2" }, "host3": { "ip": "10.1.18.1" } } } ...
RomanPerekhrest's user avatar
45 votes
Accepted

How to convert embedded (quoted) json string to json

There is a raw flag for this -r output raw strings, not JSON texts; jq -rc .stuff stuff.json Output {"date":"2018-01-08"}
OneCricketeer's user avatar
33 votes
Accepted

awk/sed/perl one liner + how to print only the properties lines from json file

Jq is the right tool for processing JSON data: jq '.items[].properties | to_entries[] | "\(.key) : \(.value)"' input.json The output: "content : \n#!/bin/bash\n\n# Set KAFKA specific environment ...
RomanPerekhrest's user avatar
31 votes
Accepted

Wrap all numerics in JSON with quotes

$ jq 'map_values(tostring)' file.json { "id": "1", "customer": "user", "plate": "BMT-216-A", "country": "GB", "amount": "1000", "pndNumber": "20000", "zoneNumber": "4" } Redirect to a ...
Kusalananda's user avatar
  • 356k
30 votes
Accepted

jq - print values in one line

Use the "join", -j $ jq -jr '.[]|"name:", " ",.name, "\n","groups:", (.grp[]|" ",.name),"\n"' test_json name: cust1 groups: BA2 GA1 NA1 TR3 TS1 And with a place holder $ jq -jr '.[]|"name:", " ",....
EchoMike444's user avatar
  • 3,225
30 votes
Accepted

looping through JSON array in shell script

For the use case provided in the Question, @JigglyNaga's answer is probably better than this, but for some more complicated task, you could also loop through the list items using keys: from file: ...
pLumo's user avatar
  • 23.2k
30 votes

How to edit a JSON file using shell?

Using the del function in jq: jq 'del(.list[] | select(.name=="APP1"))' If you wanted to pass the app name as a shell variable to jq you can use the --arg option: jq --arg name "$name&...
jesse_b's user avatar
  • 41.5k
29 votes
Accepted

Add key/value to json object

$ jq '.array[] += { new_key: 0 }' <<<"$json" { "name": "foo", "array": [ { "name": "bar", "thing": ...
Kusalananda's user avatar
  • 356k
28 votes

How to decompress jsonlz4 files (Firefox bookmark backups) using the command line?

Save this script in a file, e.g., mozlz4: #!/usr/bin/env python from sys import stdin, stdout, argv, stderr import os try: import lz4.block as lz4 except ImportError: import lz4 stdin = os....
Håkon A. Hjortland's user avatar
28 votes

awk/sed/perl one liner + how to print only the properties lines from json file

Please, please don’t get into the habit of parsing structured data with unstructured tools. If you’re parsing XML, JSON, YAML etc., use a specific parser, at least to convert the structured data into ...
Stephen Kitt's user avatar
26 votes

How to find and replace multiple field values using jq?

jq's assignment operations can perform an update on as many locations at once as you can name and are made for this sort of situation. You can use jq '(.. | .name?) |= "XXXX"' to find every ...
Michael Homer's user avatar
25 votes

Match keys with regex in jq

jq's regular expressions filters (test,match,capture) take raw text as their input. To apply a regex to key names, you'll first have to convert those key names into text. jq provides a function ...
JigglyNaga's user avatar
  • 8,096
24 votes
Accepted

jq + how to print only the value of key under properties

Try this, jq '.items[].properties.content' t.json Add -r if you want to get rid of the double quotes
pLumo's user avatar
  • 23.2k
23 votes

Why is the JSON content from heredoc not parsable?

VALUE=<<PERSON some data PERSON echo "$VALUE" No output. A here-document is a redirection, you can't redirect into a variable. When the command line is parsed, redirections are handled in a ...
Kusalananda's user avatar
  • 356k
23 votes
Accepted

jq - add objects from file into json array

jq has a flag for feeding actual JSON contents with its --argjson flag. What you need to do is, store the content of the first JSON file in a variable in jq's context and update it in the second JSON ...
Inian's user avatar
  • 13.1k
23 votes
Accepted

How to JSON-escape input?

jq -R -s '.' < datafile This reads in all of datafile as a string, and then has jq just print it out as a JSON string. It will give you a quoted string suitable for substituting into that template ...
Michael Homer's user avatar
23 votes

How to find value of key-value in json in bash script

The short answer: Install jq You shouldn't parse json without a json parser. To do this with jq: echo "$json" | jq -r '.access_token' or, without letting echo expand encoded tabs and ...
jesse_b's user avatar
  • 41.5k
22 votes

looping through JSON array in shell script

Extracting the members jq -c '.children.values[]|[.path.components[0],.type,.size]' .children.values[] outputs every member of the array .values. | pipes the previous result through the next filter, ...
JigglyNaga's user avatar
  • 8,096
21 votes
Accepted

Get field and nested field at the same time using jq

As a list of tab-separated values: Create an array of the wanted values for each issue[] and pass it to @tsv. $ jq -r '.issues[] | [ .key, .fields.summary ] | @tsv' file.json RM-111 6.6.0 As a ...
Kusalananda's user avatar
  • 356k
20 votes

Merge jq output into a comma separated string

If what you want is CSV-formatted output from your JSON document, then you may use the @csv operator in jq. somecommand | jq -r '[ .host_components[].HostRoles.host_name ] | @csv' This uses the same ...
Kusalananda's user avatar
  • 356k
20 votes
Accepted

How to replace a value in json file using jq and returning the whole content

The jq expression .HeaderAuthentication.Headers[] | select(.Name == "api-key") picks out the Headers array element that has api-key as its Name value. The expression (.HeaderAuthentication....
Kusalananda's user avatar
  • 356k
19 votes

How to find value of key-value in json in bash script

No jq, awk, sed: #!/bin/bash json='{"access_token":"kjdshfsd", "key2":"value"}' echo $json | grep -o '"access_token":"[^"]*' | grep -o ...
social's user avatar
  • 315
18 votes
Accepted

How do I add a variable to this curl command?

Stop the single quoted string, follow with the variable expansion, posibly double quoted, and resume the single quoted string: --data '{"text": "'"$variable"'"}' ($...
Petr Skocik's user avatar
  • 29.7k
18 votes
Accepted

How to print path and key values of JSON file

I'm not super knowledgeable on jq but I found this on the interwebs and I think it will work in your case: Cut and paste version: jq -r 'paths(scalars | true) as $p | [ ( [ $p[] | tostring ] | join(&...
user1794469's user avatar
  • 4,237
18 votes
Accepted

Add JSON objects to array using jq

This trick with the jq 1.5 inputs streaming filter seems to do it ... | jq -n '.items |= [inputs]' Ex. $ find ~/ -maxdepth 1 -name "D*" | while read line; do jq -n --arg name &...
steeldriver's user avatar
  • 83.8k
18 votes
Accepted

Convert JSON array into CSV

The issue is not really that the JSON that you show is an array, but that each element of the array (of which you only have one) is a fairly complex structure. It is straight forward to extract the ...
Kusalananda's user avatar
  • 356k

Only top scored, non community-wiki answers of a minimum length are eligible