Questions tagged [jq]

Questions about the command line JSON processing tool jq.

jq is a command-line JSON processor for slicing, filtering, mapping and transforming structured JSON data.

Further reading

382 questions
16
votes
2 answers

How can I exclude all keys with a specific value inside a JSON with jq?

Let's say I have a JSON like the following: { "key1": { "keyA": "1", "keyB": "null", "keyC": "null" }, "key2": { "keyA": "null", "keyB": "3", "keyC": "null" } } I'd like to find a way…
raylight
  • 461
  • 1
  • 6
  • 15
15
votes
1 answer

How to merge arrays from multiple json files with JQ?

I have multiple JSON Files with the same structure , the list is the array. $ jq 'keys' file_1.json [ "itemsPerPage", "links", "list", "startIndex" ] the list field looks like this file1.json "list" : [ {"id: 123, "fname":"SAM" }, {"id:…
Satish
  • 343
13
votes
2 answers

How does jq work with keys containing slash?

My test json file is as follows: { "server":"xxx", "cert":"line1\nline2", "security/path": "/var/log/systems.log" } I would like to filter by key security/path, and below commands all don't work. jq .security/path test.json jq: error:…
fhcat
  • 231
13
votes
1 answer

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": [ { "value": "90", …
sam
  • 171
10
votes
1 answer

jq get attribute of nested object

I have the following structure (full file example here): { { "weather": [ { "advertiser": "Worldwide Forecasts", "notificationText": "Weather - Check the Weather Now - Weather", "notificationURL":…
6
votes
1 answer

how do I fix a jq expression returning Invalid path expression error?

I have a package.json file that looks like this: { "name": "service", "version": "1.0.0", "private": true, "description": "my service", "license": "none", "scripts": { "build": "tsc" }, "dependencies": { …
5
votes
1 answer

jq - select an attribute based on a key that starts with or contains a string

input.json { "Stack": { "KeypairNameB651C0C1": "key-0123456abcdefg", "AsgNameA7D05B90": "my-asg-name" } } The key names could vary, but will always begin with a set string Similar question here but want to somehow get the value based on…
tkwargs
  • 153
5
votes
2 answers

get unique without sorting in jq

Array => ["cross", "base", "cross", "dunk"] I want to delete all repeated values without sorting. Command: jq '. | unique' file.json Output: ["base", "cross", "dunk"] Expected Output: ["cross", "base", "dunk"]
decipher
  • 302
5
votes
1 answer

Parent object key of array that contains value with jq

Say I have data something like this: { "18" : [ 2, 3, 3 ], "28" : [ 2, 2, 7 ], "45" : [ 3, 3, 5 ] } I'd like to make a jq query that returns the key/keys of the object with the array that contains a given value. For example, 2 exists in keys…
A. Que
  • 633
5
votes
3 answers

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", "realm": "internal" }, { "name": "C_C", …
5
votes
1 answer

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": [ { "id": 123 }, { …
MHogge
  • 237
4
votes
2 answers

Displaying "current" key in jq

I have a dictionary keyed on usernames, like: "userinfo": { "alice": { "key1": 1, "key2": 2}, "bob": { "key1": 11, "key2": 22}, ... } I want to display tabular text of the username and some values. alice 1 2 bob 11 22 It's easy enough to get the…
4
votes
1 answer

How to convert timestamp value to number and then convert to human readable

I have a string timestamp key I need to convert to a number because strftime throws an error that it needs to be a number. journalctl -n1 --output=json | jq '.__REALTIME_TIMESTAMP | tonumber |= (. / 1000 | strftime("%Y-%m-%d")), .MESSAGE' but I get…
deanresin
  • 577
  • 1
  • 5
  • 23
4
votes
2 answers

Keep escaped forward slashes in JSON string values with jq

According to the JSON specification, forward slashes don't have to be escaped with a backslash but they can be. I have a JSON file which has all forward slashes in string values escaped for compatibly reasons (but not inside the keys): { …
YourMJK
  • 151
4
votes
2 answers

Use jq to retrieve fields specified on command line

Given this input { "attributes": { "f1": "one", "f2": "two", "f3": "three" } } I'd like to be able to specify on the jq command line which fields to retrieve and spit their values out space-delimited on the same line. jq -r…
SteveT
  • 143
  • 1
  • 4
1
2 3 4