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.

Filter by
Sorted by
Tagged with
52
votes
9answers
244k views

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": [],...
50
votes
6answers
91k views

Using jq to extract values and format in CSV

I have the below JSON file: { "data": [ { "displayName": "First Name", "rank": 1, "value": "VALUE" }, { "displayName": "Last Name", "rank": 2, ...
42
votes
7answers
159k views

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?
29
votes
3answers
32k views

Limit grep context to N characters on line

I have to grep through some JSON files in which the line lengths exceed a few thousand characters. How can I limit grep to display context up to N characters to the left and right of the match? Any ...
25
votes
1answer
16k views

How to prettyprint json using jq standalone?

Currently, when I want to pretty-print a json file using jq, I use: cat file.json | grep jq . Yet I would expect for jq to accept a file name as argument without having to fallback on cat. The man ...
20
votes
2answers
11k views

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 ...
17
votes
8answers
54k views

Parse JSON using Python?

I have a JSON file members.json as below. { "took": 670, "timed_out": false, "_shards": { "total": 8, "successful": 8, "failed": 0 }, "hits": { "total": 74, ...
12
votes
5answers
34k views

How to extract data from a JSON file

I have bin searching a solution for my question but didn't find a or better said I did not get it with what I found. So lets talk about what my problem is about. I am using a Smart Home Control ...
11
votes
4answers
3k views

Why is the JSON content from heredoc not parsable?

I have a JSON fragment. The following does not work: VALUE=<<PERSON { "type": "account", "customer_id": "1234", "customer_email": "jim@gmail.com" } PERSON echo -n "$VALUE" | python -m ...
11
votes
4answers
28k views

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": [ { "...
10
votes
5answers
2k views

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

how to print only the properties lines from json file example of json file { "href" : "http://master02:8080/api/v1/clusters/HDP/configurations?type=kafka-env&tag=version1527250007610", "...
10
votes
3answers
18k views

Merge jq output into a comma separated string [duplicate]

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-...
9
votes
4answers
2k views

Wrap all numerics in JSON with quotes

There are JSON data which contains some numeric values. How to convert all numerics to strings? (wrap with quotes) Example: { "id":1, "customer":"user", "plate":"BMT-216-A", ...
9
votes
1answer
6k views

can't find documentation on json.tool [closed]

I can find scattered articles online that use json.tool to produce pretty python, but no clear documentation on usage. I checked docs.python. but there is no formal documentation of json.tool. I ...
8
votes
6answers
9k views

Convert output of tree command to json format

Is there a convenient way to convert the output of the *nix command "tree" to JSON format? Edit: I think I didn't describe my problem well enough. My goal is to convert something like: . |-- dir1 | ...
8
votes
2answers
8k views

JSON parsing on the shell

How can I parse JSON output on the shell? For example, Amazon Web Services provides a CLI to retrieve the status of your instances: $ aws ec2 describe-instances <my_instance_id> But the ...
8
votes
3answers
21k views

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 ...
7
votes
2answers
11k views

Is there a shell one-liner to convert JSON to BSON?

Is there a shell one-liner to convert JSON to BSON? Something suitable for one-off execution, or a shell script? bsondump from MongoDB goes the inverse way, but I'm clueless as to what I can use to ...
7
votes
3answers
4k views

How to find and replace multiple field values using jq?

In the following json file, { "email": "xxx", "pass": "yyy", "contact": [ { "id": 111, "name": "AAA" } ], "lname": "YYY", "name": "AAA", "group": [ { "...
6
votes
1answer
3k views

jq - print “-” for null values

input json: { "id": "3885", "login": "050111", "lastLoginTime": 1529730115000, "lastLoginFrom": "192.168.66.230" } { "id": "3898", "login": "050112", "lastLoginTime": null, "...
6
votes
1answer
16k views

Editing a child JSON element value with jq

I'm trying to use jq in order to change a child's value. For instance, I want to change the value of "test2" from ["yo", "bye"] to ["hi"]. { "title": "hello", "body": { "test1": 123, ...
6
votes
3answers
995 views

Why does `cat` seem to iterate over JSON arrays?

I'm puzzled by this behaviour of cat when trying to output a heredoc containing JSON in bash 3.2: input: $ cat <(cat <<EOF > {"x":[{"a":1,"b":2}]} > EOF) output: {"x":["a":1]} {"x":[...
6
votes
2answers
8k views

jq - add objects from file into json array

I want to add an array with elements and value into an existing json file using jq. I already have a file (input.json) with { "id": 9, "version": 0, "lastUpdTs": 1532371267968, "name": "...
6
votes
6answers
6k views

How to loop through json file?

I have a below json file and I want to get the hostId, only if the name contains some specific value. I want to use shell script to achieve this. { "items" : [ { "name" : "first-block-...
5
votes
5answers
6k views

looping through JSON array in shell script

Below is the curl command output (file information about branch), need script or command to print file name, filetype and size. I have tried with jq but was able fetch single value ( jq '.values[]....
5
votes
5answers
6k views

Is there a CLI tool that would prettify a JSON string

On my host I enter the following, which returns a bunch of information which isn't particularly easy to read at the CLI. echo $ENV_VAR | base64 --decode Is there a way to format it? This is a ...
5
votes
2answers
2k views

how to convert text file to json string (replace newlines with “\n”)

we have the following file : more value.js "\n#\n#\n# Licensed to the Apache Software Foundation (ASF) under one\n# or more contributor license agreements. See the NOTICE file\n# distributed with t ...
5
votes
2answers
21k views

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": "...
5
votes
5answers
705 views

parse colon separated value pairs

I've written a script that converts nmcli --mode multiline dev wifi into json but I'm finding it's inconsistent (breaks when results have a space), long, and hard to read. I'm wondering if it is ...
5
votes
3answers
4k views

How can I pretty format a JSON file, with all the correct indents and everything?

I have a JSON file on CentOS where all text is on the same line. How can I pretty format it with all the correct indents and everything?
4
votes
5answers
19k views

How to retrieve values from json object using awk or sed [duplicate]

I have the following json [root@mdfdevha1 ~]# echo "$Group_ID" [ { "id" : "e27206c0-aeb6-43db-acda-c4ba43233071", "name" : "A1", "path" : "/A1", "subGroups" : [ ] }, { "id" : "89f3bd6a-...
4
votes
4answers
8k views

Command to remove a portion of JSON data from each line?

I have test.json file with different lengths of rows. Some fictitious example: { a: 123, b: sd, c: x45, d: 1, e: '' } { a: 5, b: bfgg, c: x4c, d: 31, e: '' } I want to cut the whole substring after ...
4
votes
1answer
7k views

Convert JSON to CSV [closed]

JSON file: "UserDetailList": [ { "UserName": "citrix-xendesktop-ec2-provisioning", "GroupList": [], "CreateDate": "2017-11-07T14:20:14Z", "UserId": "1234556", "Path": "/", "...
4
votes
3answers
2k views

Convert /proc/cpuinfo to a JSON in linux bash

I'd like to convert the entire entry of a cat /proc/cpuinfo to a JSON of "key" : "value". Is this possible? I tried to explode the string using \n in hopes that I could get each line and then I could ...
4
votes
1answer
5k views

Wildcard in jq with comparatives

Piping in some json, I want to be able to add a wildcard to the comparative, so: curl example.com/json | jq 'select(.[].properties.type == "dev*")' So it would print out anything with a type that ...
4
votes
1answer
828 views

XMLstarlet for JSON?

Is there a tool that is similar to XMLstarlet but works on JSON instead of XML? My usecase: extract certain values from hierarchical JSON documents, as they are returned by some REST APIs. Something ...
4
votes
1answer
3k views

Match keys with regex in jq [closed]

I'm writing a script that analyses JSON input (jsonfile): { "key11":1010,"key11_yes":13,"key11_no":14, "key12":12120,"key12_yes":9,"key12_no":25, "key13":12103,"key13_yes":13,"key13_no":20 ...
4
votes
4answers
6k views

Alternative for jq in debian wheezy to parse json in bash

In Ubuntu you can simply install jq apt-get install jq but on debian it is not in the repository. Is there an alternative commandline to parse a JSON string on the console? maybe you can achieve ...
4
votes
2answers
5k views

Split a sting field into an array in jq?

I have a json array returned from curl that looks like this: [ { "title": "Some Title", "tags":"tagA tag-B tagC" }, { "title": "Some Title 2", "tags":"tagA tagC" }, ... ] I'd like ...
4
votes
2answers
13k views

Curl JSON encoded in UTF-8

Important note: I'm using Cygwin I retrieve JSON from a file that I alter before sending it to a server using curl. By now everything is ok except one thing, when the server receive the JSON, every ...
3
votes
4answers
684 views

Can I grep for 2 pattterns and have them listed side by side?

Ubuntu 16.04 bash -version GNU bash, version 4.4.0(1)-release (x86_64-unknown-linux-gnu) I would like to grep for 2 patterns and then have them listed side by side. ATM, this is what I have: root@...
3
votes
1answer
13k views

jq print key and value for all in sub-object

I found this Q/A with the solution to print all the keys in an object: jq -r 'keys[] as $k | "\($k), \(.[$k] | .ip)"' In my case I want to perform the above but on a sub-object: jq -r '....
3
votes
4answers
4k views

How do I convert the output of ps(1) to JSON?

I want to convert the output of the command ps to JSON in order to process it as structured data (with jq in this particular case). How do I do that? The output looks like the following: PID TTY ...
3
votes
1answer
874 views

How to combine strings from JSON values, keeping only part of the string?

I have sample: "name": "The title of website", "sync_transaction_version": "1", "type": "url", "url": "https://url_of_website" I want to get the following ...
3
votes
2answers
5k views

jq - print values in one line

json input: [ { "name": "cust1", "grp": [ { "id": "46", "name": "BA2" }, { "id": "36", "name": "GA1" }, { "id": "47", ...
3
votes
6answers
987 views

update the json file

I have a file, actually json file. How I can add a comma to the end of every but the last line ? For example: {"a":1, "b":1, "c":2} {"a":3, "b":3, "c":1} {"a":1, "b":2, "c":3} What I want: {"a":1, "...
3
votes
3answers
732 views

how to parse specific values from multiple JSON objects into csv using jq

I have following json [ { "ip":"105.105.105.105", "timestamp":"1543746097", "ports":[ { "port":80, "proto":"tcp", "status":"open", ...
3
votes
1answer
428 views

Extract .json from a text file with arbitrary text

I have an output from a program that give some arbitrary text, with .json stuff inside like: blablablabla blablab some more text blablablabla blablab some more text { "glossary": { "...
3
votes
1answer
7k views

How to escape new line characters for json?

I have a file that contains new line characters. I am posting the file via curl to a server that would parse it as json. It rejects the request due to the new line characters. But when I do: $(echo ...
3
votes
1answer
3k views

jq - select hostname starting from 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 ...