Questions tagged [json]

JSON (JavaScript Object Notation) is a lightweight data representation format that resembles JavaScript source and that is can be easily red by machines and edited by humans with some effort.

JSON (JavaScript Object Notation) is a lightweight text-based data representation format. It coincides with JavaScript syntax. It aims at being both human-readable and machine-readable, although its readability for humans is hindered by excessive use of quotes and not enforcing indentation so hierarchical data structures are often not recognised as such.

Although a textual format, the complexities of free formatting make JSON difficult if not impossible to parse with awk, sed etc, unless severe formatting restrictions are imposed that are not part of the JSON specification. It is generally recommended to use a proper JSON parser to handle extraction and manipulation of JSON files, even if (currently) your files are so restricted that line oriented tools might work.

Useful resources

commandline tools

  • json_pp
  • yaml (JSON is a subset of YAML 1.2)
516 questions
138
votes
1 answer

How to prettyprint json using jq standalone?

Currently, when I want to pretty-print a json file using jq, I use: cat file.json | jq . Yet I would expect for jq to accept a file name as argument without having to fallback on cat. The man page says: jq [options...] filter [files...] ... By…
k0pernikus
  • 14,853
  • 21
  • 58
  • 79
95
votes
8 answers

Using jq to extract values from column-oriented JSON and format in CSV

I have the below JSON file, with the data stored as columns enumerated by rank in an array: { "data": [ { "displayName": "First Name", "rank": 1, "value": "VALUE" }, { "displayName": "Last Name", "rank":…
Kerim
  • 951
  • 1
  • 6
  • 3
88
votes
13 answers

How to parse JSON with shell scripting in Linux?

I have a JSON output from which I need to extract a few parameters in Linux. This is the JSON output: { "OwnerId": "121456789127", "ReservationId": "r-48465168", "Groups": [], "Instances": [ { …
user3086014
  • 981
  • 1
  • 7
  • 6
85
votes
1 answer

jq - select an attribute beginning with a string

input json: [ { "id": "89", "hostname": "abcd" }, { "id": "89", "hostname": "babcd" } ] How to modify below filter to get on output only hostname beginning with "abcd"? $ jq -r '.[]|select(.hostname | contains("abcd"))'…
Chris
  • 3,591
  • 7
  • 24
  • 35
68
votes
4 answers

Merge jq output into a comma separated string

I am trying to curl some URL which returns a json file, then I want to parse hosts from it and create a comma separated string. I have the first part working curl -s -u "admin:admin" -H "X-Requested-By: ambari"…
roy
  • 1,329
  • 2
  • 12
  • 13
60
votes
2 answers

How to convert embedded (quoted) json string to json

I'm familiar with "jq" for parsing json. I work with one service that produces a json response where one of the properties is itself a json string. How do I convert that quoted value to a valid json string so I can then process it with jq? For…
David M. Karr
  • 891
  • 1
  • 7
  • 16
50
votes
8 answers

How do I count the number of occurrences of a word in a text file with the command line?

I have a large JSON file that is on one line, and I want to use the command line to be able to count the number of occurrences of a word in the file. How can I do that?
mythz
  • 611
  • 1
  • 5
  • 8
47
votes
4 answers

Setting jq output to a Bash Variable

I'm using curl to get JSON back from a rest api like this: content=$(curl -s -X GET -H "Header:Value" http://127.0.0.1:8200/etc) echo "${content}"| jq -r '.data.value' which produces the value I need. However; when I change the above code to look…
jymbo
  • 955
  • 1
  • 7
  • 7
38
votes
7 answers

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

There seems to be various JavaScript+browser specific ways of decompressing this, but isn't there some way to transform jsonlz4 files to something unlz4 will read?
l0b0
  • 50,672
  • 41
  • 197
  • 360
31
votes
6 answers

parse one field from an JSON array into bash array

I have a JSON output that contains a list of objects stored in a variable. (I may not be phrasing that right) [ { "item1": "value1", "item2": "value2", "sub items": [ { "subitem": "subvalue" } ] }, { …
JpaytonWPD
  • 648
  • 1
  • 6
  • 16
28
votes
2 answers

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

I'm starting with json that looks like this: { "object": "list", "data": [ { "id": "in_1HW85aFGUwFHXzvl8wJbW7V7", "object": "invoice", "account_country": "US", "customer_name": "clientOne", "date": 1601244686, …
alec
  • 1,508
  • 3
  • 15
  • 26
28
votes
6 answers

How to extract data from a JSON file

I have been searching for a solution for my question but didn't find a or better said I did not get it with what I found.  My problem is: I am using a Smart Home Control Software on a Raspberry Pi.  Using pilight-receive, I can capture the data from…
26
votes
2 answers

jq - print "-" for null values

input json: { "id": "3885", "login": "050111", "lastLoginTime": 1529730115000, "lastLoginFrom": "192.168.66.230" } { "id": "3898", "login": "050112", "lastLoginTime": null, "lastLoginFrom": null } I want to get output for login,…
Chris
  • 3,591
  • 7
  • 24
  • 35
24
votes
6 answers

Bash variable substitution in a JSON string

I'm trying to create a JSON in BASH where one of the fields is based on the result of an earlier command BIN=$(cat next_entry) OUTDIR="/tmp/cpupower/${BIN}" echo $OUTDIR JSON="'"'{"hostname": "localhost", "outdir": "${OUTDIR}", "port": 20400,…
Guru Prasad
  • 363
  • 1
  • 3
  • 6
22
votes
2 answers

jq - print values in one line

json input: [ { "name": "cust1", "grp": [ { "id": "46", "name": "BA2" }, { "id": "36", "name": "GA1" }, { "id": "47", "name": "NA1" }, { …
Chris
  • 3,591
  • 7
  • 24
  • 35
1
2 3
34 35