Questions tagged [jq]

Questions about the command line JSON processing tool jq.

Filter by
Sorted by
Tagged with
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, ...
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 ...
12
votes
2answers
2k views

Using jq within pipe chain produces no output

The issue of jq needing an explicit filter when the output is redirected is discussed all over the web. But I'm unable to redirect output if jq is part of a pipe chain, even when an explicit filter is ...
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-...
10
votes
3answers
16k views

JSON array to bash variables using jq

I've got a JSON array like so: { "SITE_DATA": { "URL": "example.com", "AUTHOR": "John Doe", "CREATED": "10/22/2017" } } I'm looking to iterate over this array using jq so I can set ...
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", ...
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
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
3answers
374 views

Trim trailing zeroes off a number extracted by jq

The following command achieve my goal by grepping BTC price from specific exchange. curl -sS https://api.binance.com/api/v1/ticker/price?symbol=BTCUSDT | jq -r '.price' the output will be for the ...
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
1answer
6k views

combine pipe and redirect on curl and jq

If i curl to some site I can get straight json: curl http://httpbin.org/ip { "origin": "37.77.126.22"} to beautify, i do: curl http://httpbin.org/ip | jq { "origin": "37.77.126.22" } to ...
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
1answer
1k views

Error message “Cannot index array with string 'Title'” when parsing JSON data with jq

{ "content": [ { "Title": "abc", "brand": "xyz", "size": "5 g", "date": "2019-01-01", "details": { "Temperature": [ { ...
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
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
1answer
979 views

jq returning full result after operation

I'm adding a property to a JSON string with result=$(jq -c ".docs[$docIndex] + { \"value\": \"value\" }"<<<"$fileContent") Where the JSON inside $fileContent is { "docs": [ { ...
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
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
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
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 ...
3
votes
1answer
833 views

if else in jq is not giving expected output

I need to display users if their realm is internal. Input: [ { "name": "A_A", "uri": "https:test/test", "realm": "internal" }, { "name": "B_B", "uri": "https:test/test", ...
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
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
1answer
5k views

bash add property using variable with jq

To add a property to a JSON text I manage to use result=$(jq -c '.docs[0] + { "value": "someValue" }'<<<"$fileContent") But if I want to replace "someValue" by a variable: result=$(jq -c '....
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
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 ...
3
votes
1answer
847 views

Split a json array to multiple files

I have a file that looks like this: [ { "billingAccountNumber": "x", "paymentResponseObject": { "uiErrorDipslayMessage": "", "transactionStatus": "S", "...
3
votes
2answers
741 views

parse JSON and malipulate its object values

I have a json file named xyz.json as follows [ { "annotations": [ "a" , "b" , "c" ], "class": "image", "filename": "vc00_02201.png" }, { "annotations": [], ...
3
votes
2answers
993 views

How to replace multiple lines with “sed” and convert to JSON?

{ "auth": 'log', "appid": 21, "custid": "599c1f910f53ada8468b4567", "hwid": "59e719ba0f53adfd6a8b4597" } 24/10/2017 12:44:24 -------------------------------------------------------------------...
3
votes
1answer
549 views

bash string concatenation failed

Important note: I'm using Cygwin to run this script. I got a json string by executing result=$(jq -c ".docs[$docIndex] + { \"_rev\": \"rev\" }"<<<"$fileContent") Here is the JSON I get: {"...
2
votes
2answers
916 views

Use JQ to generate CSV from knife search

JQ looks like a great tool, but I'm struggling with it. Here is what I am trying to do: Extract just the values from this chef knife search and generate a CSV. given this command and output: knife ...
2
votes
3answers
811 views

Extract json array element based on a subelement value

We have the following example file ( very long file , this is short example ) "request_status" : "FAILED" { "href" : "http://localhost:8080/api/v1/clusters/sys41/requests/333", "Requests" : { ...
2
votes
3answers
933 views

How do I invoke jq inside awk?

Essentially I have a file.log as follow blah blah blah blah Hello world | {"foo": "bar"} blah blah Hello earth | {"foo1": "bar1"} Now my goal is to write some shell commands to have desire output ...
2
votes
1answer
958 views

Why can't I delete these array items in jq?

Given the command: echo "[\"tag1\", \"[[Super\", \"Duppa\", \"Database\", \"Analyst]]\", \"tag2\"]" | jq -f ~/somefilter.jq And the filter: def hasOne(x): x | (startswith("[[") or endswith("]]") ); ...
2
votes
2answers
851 views

Using JQ to get simple delimited output

How might one turn this: { "write": { "out": 0, "available": 128, "totalTickets": 128 }, "read": { "out": 7, "available": 249, "totalTickets": 256 } } Into 0 128 128 ...
2
votes
1answer
21 views

What is the difference between vertical pipe and juxtaposition in JQ?

My program returns JSON into stdout and it is an object with operations field, which contains a list of operations. Each operation contains fields including id fields, for example: { "operations":...
2
votes
1answer
8k views

Filtering JSON request with JQ and bash scripting

I'm requesting a JSON from Twitch with: curl --silent -H 'Accept: application/vnd.twitchtv.v3+json' -X GET https://api.twitch.tv/kraken/streams/$1 where $1 is the input I send for my function. Now ...
2
votes
1answer
27 views

Replace part of a vdf file

Again I am attempting to alter configuration file via script. I looked into jq but jq seems to support the .json format alone. .vdf is almost exactly like .json except that there are no commas. ...
2
votes
1answer
400 views

jq + print properties from json file without removing the necessary backslash

we have the following json file: more file.json { "href" : "http://master02:8080/api/v1/clusters/HDP/configurations?type=kafka-env&tag=version1527250007610", "items" : [ { "href" : ...
2
votes
2answers
44 views

JQ Linux JSON Parse Unknown Key

I want to build an automated script which builds an new VPS Server on the page vultr.com. They also have an API Access and I could create it and receive also anything I need. The problem is, vultr ...
2
votes
0answers
83 views

JSON -> csv creating header line and padding header if found empty field

I have a programme in bash that get JSONline files with several million of these object per line (See source) { "company_number": "09626947", "data": { "address": { "address_line_1": "...
2
votes
0answers
229 views

process JSON format with JQ tools [closed]

I want check format keys in bash script. In my json if any keys match this format key[0-9][0-9]_$ my script exit 1 else exit 2. I want use JQ tools. Bash: #!/bin/bash json=$(cat <<EOF {"key11"...
1
vote
1answer
4k views

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

we have the follwing json file more t.json { "href" : "htr", "items" : [ { "href" : "lpo", "tag" : "version1533203561827110", "type" : "kafka-log4j", "version" : 6, ...
1
vote
2answers
2k views

Identify .json files with empty arrays?

I have a folder with multiple .json files. There are certain files with empty arrays, Sample file : { "WarehouseActivity": [] } The file has no other data apart from the one shown above. I need to ...
1
vote
1answer
217 views

Convert json file to a “key-path” with the resulting value at the end of each “key-path”

I have a large and relatively complex json configuration file that I want to search for keys and/or values using simple tools like "grep". When I 'grep' the file I would like to have in the output ...
1
vote
2answers
542 views

Loop into JSON Array in Bash Script

I make a curl request to an API and get a json return with jq. The result { "errors": [], "metadata": { "clientTransactionId": "", "serverTransactionId": "20190318164551347" }, "...