Questions tagged [jq]
jq is a sed-like tool for JSON data – you can use it to slice, filter, map, and transform structured data with the same ease that sed, awk, grep and friends lets you play with text.
6,134
questions
1
vote
2
answers
27
views
Create a hash of arrays from CSV with jq
I have the following CSV (truncated):
"adfebb","a-f-A-M-F-R-Z","95-00123","C560","USAF"
"ae0133","a-f-A-M-F-R-Z","97-00102&...
0
votes
2
answers
32
views
jq recursion of unknown levels between two select() filters
given a variable nested json, I would like to filter based on two select functions and return an array of values matching a key.
Is it possible when the recursion between say "dog" and "...
0
votes
3
answers
71
views
Using arithmetic operation in jq adding values doesn't work
I would like to enter few fields into json file from the script in bash, where I create fresh file if empty and then adding values from FOR loop. I have following statements
JSFl="/tmp/test.json&...
0
votes
1
answer
31
views
How to select json array items with jq where a constant string ends with the array item?
Given a JSON:
{
"HostedZones": [
{
"Id": "/hostedzone/AAAA",
"Name": "example.com."
},
{
...
-1
votes
0
answers
51
views
jq: filter array of objects based on condition
I have an array of objects with a tab_id and pane_id fields.
I would like to return the tab_ids of the first objects whose pane_id filed matches the value of environment variable
I am also interested ...
0
votes
1
answer
46
views
Update value in json file from commandline by preserving formatting and comments
I am trying to update some values in my Visual Studio Code settings.json from command line to be used in some desktop shortcuts and scripts in my Linux desktop. I was going to use jq but it looks like ...
0
votes
2
answers
62
views
Can I reduce this JQ expression
Here is my input:
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
lava1 pts/0 157.48.149.102 05:03 31.00s 0.31s 0.31s -bash
azureuse pts/1 157.48.149.102 07:26 ...
2
votes
2
answers
82
views
+250
Print keys in yaml, so that they can be used for jsonpath
echo "apiVersion: v1
kind: Node
metadata:
name: host-cluster-control-plane-64j47
labels:
beta.kubernetes.io/arch: amd64
" | yq -o p
Result:
apiVersion = v1
kind = Node
metadata.name ...
0
votes
1
answer
39
views
JQ merging arrays using expression and not slurp
I have this data:
[
"USER",
"TTY",
"FROM",
"LOGIN@",
"IDLE",
"JCPU",
"PCPU",
"WHAT"
]
[
"lava1&...
0
votes
0
answers
9
views
Extract specific field from JSON file using jq [duplicate]
I am trying to extract a specific field from some JSON output using jq but am getting some syntax errors. I'm hoping this is just a rookie mistake. Here is the JSON output:
{"data":[{"...
0
votes
1
answer
28
views
Find pods with old container that needs to be restarted
I have the following minimal example of a pod list:
{
"items": [
{
"metadata": {
"name": "app"
},
...
0
votes
0
answers
23
views
jq not working with variables, throws error "jq: error: $var is not defined at <top-level>" [duplicate]
let var=5*86400
echo $var
Output - 432000
Trying to execute this variable with jq command
jq --null-input '$var'
Output -
jq: error: $var is not defined at <top-level>, line 1:
$var
jq: 1 ...
-1
votes
1
answer
62
views
How to merge nested stacked arrays (matrices) to CSV
Is it possible to transform this JSON
echo -e '{"a": [[1,7],[2,6]],"b": [[1,3],[3,4]]}' | jq ...
In to this
k, a, b
1, 7, 3
2, 6, -
3, -, 4
Specifically the first column is the ...
1
vote
3
answers
62
views
How to zip arrays with jq and a custom zipping function
I have this data:
[
[
"USER",
"TTY",
"FROM",
"LOGIN@",
"IDLE",
"JCPU",
"PCPU",
"WHAT"
...
1
vote
1
answer
44
views
JSON Parsing with JQ
Original JSON
{
"twitter.com": [
{
"source_type": "Search",
"visits": [
{
"date": "2023-01-01",
...
1
vote
5
answers
79
views
how to round up to 2decimal in combination with %s in bash
Team,
might be simple but am scratching head. if you see I am getting 0.00 instead of 59.42. also , I want to smartly set in the %.2f inside the first var itself so that I don't have to do it ...
0
votes
1
answer
27
views
jq: reshaping with additional statistics on the sub-arrays of an array of objects - cmake compile_commands.json
I'm working with jq to process cmake generated compile_commands.json files, which are very simplistic and don't contain direct information about the targets or an easy way to see what files are ...
-2
votes
1
answer
38
views
How to match semver regex? [closed]
I'm trying to parse some json field which contains a semantic version. I'm trying the semver regex for that from the semver site.
Semver regex:
^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\...
0
votes
1
answer
56
views
cannot parse json with jq to store value in two vars
Team,
half way done but cannot figure out what am I missing. new to jq. searched google too.
json from curl response is below
curl -u ${TOKEN}: $url | jq -r '.projectStatus'
{
"status": &...
0
votes
1
answer
46
views
JQ see if a value exists
Having a JSON object like:
[{"My$Val":[{"Sub1":"val1", "pld":"s"}]},{"My$Val":[{"Sub1":"val2", "pld":"q&...
0
votes
1
answer
30
views
jq select based on two conditions within one child object
I have a json file with the following format
{"id": "3DFD4GF", "demographic": [{"country": "Spain", "rating": 7},{"country": "...
0
votes
2
answers
49
views
Unable to filter json output based on date field
I have some json data, where i need to filter the output based on given date field
from below json data i need to fetch only the block which satisfy my condition. I tried below but fetching all the ...
0
votes
1
answer
26
views
Replace value in JSON file with jq
For flexibility reasons my file is layed out as an array of keys and values with different types:
{
"keyPairs": [
{
"key": "first",
"value": {
&...
0
votes
1
answer
52
views
Pretty-print nested json with jq
Using jq, how do I pretty-print
echo '{ "my_key": "my_val", "next_key": "next_val", "schema": "{\n \"name\": \"bucket_name\"\...
0
votes
2
answers
31
views
Add default value for nested object and remove duplicates from nested array in JSON using jq
I have the following JSON:
{
"foo1": "bar1",
"foo2": "bar2",
"items": [
{
"name": "c1",
"version": ...
1
vote
1
answer
40
views
How to replace a value in json file with a new value using a variable
I want to replace the value for id with a new one.
I have the following json file.
[
{
"adminConsentDescription": "default",
"adminConsentDisplayName": "...
0
votes
2
answers
49
views
Jq: how to extract from map of maps?
I have
{
"version": 2,
"clients": {
"c1": {
"updated": "0001-01-01T00:00:00Z",
"instances": {
...
3
votes
2
answers
65
views
jq: how does any() real work in array context?
jq produces unexpected results when any() is called in this context any(array[]==val)
I'm not sure how jq calculates these results:
$ jq -n '[2,2] | any( [1,2][]|. == .) '
false
$ jq -n '[2,2] | any( [...
0
votes
3
answers
85
views
JQ Map to String
I'm trying to transform a map to a string of key/value pairs with JQ.
I'd like to transform this:
{
"foo": "bar",
"baz": "qux"
}
Into this:
-var="foo=...
1
vote
3
answers
59
views
How to extract multiple properties from a single level JSON and assign them to the respective bash variables in the most concise manner?
Suppose we have the following JSON - {a:1,b:"x"}. I want to extract the properties a and b and assign them to the respective bash variables.
Here is how I do it today:
/c/Temp$ export $(jq -...
2
votes
1
answer
34
views
Trying to transform over-complicated array of objects in JSON using JQ
I've got a JSON document that looks like this:
{
"name": "My Team",
"team size": [
{
"min": [
{
"Leader": 1
},
...
0
votes
2
answers
40
views
jq: modify string values based on values of keys in parents
I have a JSON structure like this:
{
"misc": {
"color": "blue",
"id": "yyyy-12345",
"birthdate": "2020-12-...
2
votes
2
answers
28
views
jq possible to use has() with nested keys?
is it possible to use the jq has() function with nested keys
jq -r -c 'has("a.b")' <<< '{"a":{"b":"hello"}}'
this returns as false, I have also tried
...
1
vote
2
answers
34
views
Edit objects in json array based on strings in another array with jq
For some reason I just can't wrap my head around jq. I've been going around in circles for hours trying to accomplish what I think is not too difficult a task.
With json consisting of two arrays as ...
0
votes
1
answer
20
views
How to flatten array of objects in jq
I have an array of objects in a json file:
{
"Items": [
{
"LOCATION": {
"NAME": "Dallas"
}
},
{
"LOCATION": {
...
0
votes
2
answers
49
views
How do I pass a list of filenames from find to jq?
The command find . -name 'reddit.*.flattened.json' | jq '.' gives the error parse error: Invalid numeric literal at line 2, column 0.
Motivated by this question, I tried jq --arg.
But, find . -name '...
0
votes
1
answer
31
views
Unable to resolve an array in bash using jq
I'm trying to put an array in the inside() function. If I hard code like this inside(["test, coverage"]) then my code works fine. However if I try to put it dynamically using variable called ...
0
votes
1
answer
40
views
Bash script using jq: --arg value not utilized with script
I have a bash script where a parameter is passed in which I am trying to use in a subsequent jq query, but I cannot get it to work. The command works when using manual inputs in cli, but not when ...
-1
votes
3
answers
47
views
Conditional parsing of Nested Jsons with jq
Need help parsing the below nested Json. The end goal is to get the quota limits from GCP that have non-default values. Below is the sample data for one of the services (compute APIs for a given CPU ...
-1
votes
1
answer
35
views
Reverse substring match in jq array
Let's say I have an array from file /some/file.json:
[
"This.FilE",
"That.file",
"Another.FIle"
]
Then I have a string x with value ThiS.FIle.is.HeRe, now I ...
0
votes
2
answers
28
views
jq: output structure, shortening array, preserving the other fields in the input
For a JSON file like this:
{
"mylist": [...],
"foo": "bar"
}
with mylist containing many elements, how can I output the same JSON structure, but with only the first ...
0
votes
2
answers
38
views
How do I parse a single record or an array of records in a json field using JsonPath
I have a json value that can contain a single record or an array, I need to extract the ID field of these records and gather them in an array. The resulting element should be an array with 1 or more ...
2
votes
2
answers
51
views
Data Cleaning Badly Designed JSON Data -- Need advice on best strategy
I have over 1,000 JSON files representing game elements. This is a typical example:
{
"name": "Reeve Hunter",
"edition": 3,
"revision": "2021 ...
1
vote
1
answer
51
views
ETCD export as json and decode from base64 all key/values to human readable
Is there any easy command line option to export my entire ETCD database to json file but also decode the keys and values automatically from base64?
What I succeeded to the moment is this(example show ...
0
votes
1
answer
45
views
Unable to perform if equal by using a jq output
I have an issue with my condition if by using a jq output.
So,
I have a curl to find es snapshot
$get_snap=$(curl -s "https://opens.endpoint.com/_snapshot" | jq -e '[.[].settings.bucket][1]')...
-1
votes
1
answer
48
views
"Invalid numeric literal" using jq and openai api
I'm quite new to programming, so forgive me if my question is silly.
I'm working on a bash script to translate copied text using xclip and openai.
I'm trying to pipe openai's response (not including ...
0
votes
1
answer
32
views
How to get jq output without extra characters?
When i can get result in jq with this input data
await CLI.mount({
name: "test.json",
data: JSON.stringify([{some:{data: {test: 'data'}}}, {some: {data: "there"}}])
});
I ...
1
vote
1
answer
66
views
racket pretty print json
This is what I'm using to pretty-print json (calling the jq command):
#lang racket
(require racket/string)
(require racket/system)
(define (pretty-print-json json_str)
; for escaping '\' and '"...
3
votes
2
answers
72
views
jq select compare case insensitive
I have a json file that looks like the following:
{
"items": [
{
"id": "some_id_1",
"name": "job_get_re_Log",
...
1
vote
1
answer
64
views
jq performance for very large file
I'm trying to use jq to parse the wikidata dump, which is approximately 1.5Tb of a single JSON array (each item within the array is separated by a newline though). I have some complex filtering ...