Skip to main content
Share Your Experience: Take the 2024 Developer Survey
159 votes
Accepted

jq - select an attribute beginning with a string

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

Merge jq output into a comma separated string

Do it in jq, but see @Kusalananda's answer first jq -r '.host_components[].HostRoles.host_name | join(",")' No, that's wrong. This is what you need: jq -r '.host_components | map(....
glenn jackman's user avatar
104 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
57 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,175
56 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,192
44 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
43 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
39 votes

Setting jq output to a Bash Variable

Changed the code to this and it worked: content=$(curl -s -X GET -H "Header:Value" http://127.0.0.1:8200/etc) username=$( jq -r '.data.value' <<< "${content}" ) echo &...
jymbo's user avatar
  • 1,025
35 votes

Merge jq output into a comma separated string

paste is the best tool to do this job: your_command | paste -sd, -
cuonglm's user avatar
  • 154k
34 votes
Accepted

Bash variable substitution in a JSON string

JSON=\''{"hostname": "localhost", "outdir": "'"$OUTDIR"'", "port": 20400, "size": 100000}'\' That is get out of the single quotes for the expansion of $OUTDIR. We did put that expansion inside double-...
Stéphane Chazelas'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
  • 337k
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
  • 22.6k
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
  • 37.4k
29 votes
Accepted

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

I was able to unpack the jsonlz4 by using lz4json: apt-get install liblz4-dev git clone https://github.com/andikleen/lz4json.git cd lz4json make ./lz4jsoncat ~/.mozilla/firefox/*/bookmarkbackups/*....
Rolf's user avatar
  • 306
29 votes
Accepted

Split a string field into an array in jq?

You're making it a lot more complicated than it is. Just use map() and |=: jq 'map(.tags |= split(" "))' file.json Edit: If you want to handle entries without tags: jq 'map(try(.tags |= split(" ")...
Satō Katsura's user avatar
29 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,175
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
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
  • 7,896
25 votes
Accepted

Add key/value to json object

$ jq '.array[] += { new_key: 0 }' <<<"$json" { "name": "foo", "array": [ { "name": "bar", "thing": ...
Kusalananda's user avatar
  • 337k
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
  • 22.6k
24 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
23 votes

can't find documentation on json.tool

If you look through the formal documentation of the python JSON library you see that the invocation of json.tool should be python -mjson.tool. This indicates that the program in the file tool.py under ...
Anthon's user avatar
  • 79.5k
22 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
  • 337k
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
  • 7,896
21 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
  • 12.8k
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
  • 337k
20 votes
Accepted

Editing a child JSON element value with jq

(Posting @glennjackman comment as a community answer to prevent system from autodeleting the question) jq '.body.test2 = ["hi"]' will do it
20 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
  • 37.4k

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