Add new element stored in a variable to existing JSON array with jq
I am working on creating an adaptive card dynamically. I have the skeleton on the card in JSON and I want to add entries to it (each entry will be a JSON object inside the "body" of the below schema)....
View ArticleWhat is the internal, inverse operation of .[] in jq? or How to construct an...
echo '[ {"a":1}, {"b":2}, {"c":3} ]' | jq '.[] | revert_op 'What can we replace revert_op with to get this?[ {"a":1}, {"b":2}, {"c":3} ]jq -s '.' does the job, but this is a CLI option, and I want a...
View ArticleHow to process subelements of array created by split?
I am parsing output from the docker container ls --format json command and trying to process and format the content to keep only what I need./usr/bin/docker container ls --format json | jq ". |...
View ArticleUsing jq how to correctly read a pair containing a backslash character?
I have a script with reading from json file in this way:jq -c '.[]' input.json | while read pair; do echo "$pair" | jq -r '.[0]'done;The above throws `parse error: Invalid numeric literal at line 1,...
View ArticleHow do I sum all numbers from output of jq
I have this command that I would like to sum all the numbers from the output. The command looks like this$(hadoop fs -ls -R /reports/dt=2018-08-27 | grep _stats.json | awk '{print $NF}' | xargs hadoop...
View ArticleUsing jq with Jenkins pipeline
In my use case i'm running a pipeline which calls aws cli via the withAWS plugin. I use jq to get the needed data, Now the sh returns the output as String. How do i convert this output to json object...
View ArticleHow to use `jq` to obtain the keys
My json looks like this :{"20160522201409-jobsv1-1": {"vmStateDisplayName": "Ready","servers": {"20160522201409 jobs_v1 1": {"serverStateDisplayName": "Ready","creationDate":...
View ArticleHow to get key names from JSON using jq
curl http://testhost.test.com:8080/application/app/version | jq '.version' | jq '.[]'The above command outputs only the values as...
View ArticleHow to prevent yq removing comments and empty lines?
Here Edit yaml objects in array with yq. Speed up Terminalizer's terminal cast (record) I asked about how to edit yaml with yq. I received the best answer. But by default yq removes comments and empty...
View ArticleBest way to go from JSON to bash variables
I am writing a bash script, for educational purposes.So I was thinking what is the best way to parse JSON and write the JSON attributes to bash variables.In my script, I am making an API call that...
View ArticlePrint both the key and the value for a specific key-value pair [closed]
Given the below JSON:{"k1": "v1","k2": "v2"}Print the key and value for k1 as follows:"k1","v1"I have tried:cat data.json | jq '.k1'This produces an output that only contains the value of the key -...
View Article"Ternary logic" for returned value: foo, bar or error
I've got two different JSON structures to retrieve a specific object value from, basically something like this{"one": {"foo": {"bar": "baz" } }}and another like that{"two": {"foo": {"bar": "qux" }...
View Article`jq`: cannot shorten my convoluted expression
Aftercurl 'https://goldbook.iupac.org/terms/index/all/json/download' --output 0.json,jq '.terms.list | to_entries | map(select(.value.title == "impedance")) | .[].key' 0.jsondoes exactly what I need,...
View ArticleFilter empty and/or null values with jq
I have a file with jsonlines and would like to find empty values.{"name": "Color TV", "price": "1200", "available": ""}{"name": "DVD player", "price": "200", "color": null}And would like to output...
View ArticleExtract multiple nested in a single JQ run
I need to extract multiple nested fields from a json payload. Here is an example of a payload.{"data": [ {"version": 10030002,"type": "poc_request_v1","time": 1632442480,"secret_hash":...
View Articlejq getpath error: Cannot index number with string "id"
What's wrong with:jq '[paths | select(.[-1] == "id") | {"path": ., "value": getpath(.)}]'<(echo '[{ "id": 1, "child": { "id": 2 } }]')I get following error:jq: error (at /dev/fd/63:1): Cannot index...
View ArticleMerge values from a JSON file into a new key-value in another JSON file
Let's say I have two files.ip.json{"host1": "1.1.1.1","host2": "1.1.1.2","host3": "1.1.1.3"}specs.json{"host1": {"cores": 2,"memory": 4 },"host2": {"cores": 4,"memory": 8 },"host3": {"cores":...
View ArticleHow can I pad numbers with 'jq'?
I'd like to add leading/trailing zeros to strings from numbers. The resultant string needs to contain "01" or "001" and not "1".I noticed project jq-zeros, but I have jq installed from the package...
View ArticleShorthand for querying JSON string inside JSON value
To obtain 1 from the following snippet, jq '.value' t.json | jq -r | jq -r '.value' is necessary:{"value": "{\"value\":\"1\"}"}Anyone know of a more compact way? I'm on the shell (bash), so I'm happy...
View Articlehow to merge multiple json files with same structure into one json file with...
I need to merge file1.json file2.json (could be more) into onefile.json.version is always the same value in all files. however vulnerabilities array and dependency_files array values different but...
View ArticleJQ: filter and select rows from JSONL input?
Suppose I have a JSONL file:# foo.jsonl{ "arbitrary.key":"foo" }{ "arbitrary.key":"foobar" }{ "arbitrary.key":"bar" }And I would like to filter the rows of this file:# in pseudo codecat foo.jsonl | jq...
View ArticleReducing JSON with jq
I have a JSON array of Objects:[{key1: value},{key2:value}, ...]I would like to reduce these into the following structure:{key1: value, key2: value, ...} Is this possible to do with jq?I was trying:cat...
View ArticleFormat number in thousands separators with jq json cli
Given {"a": 1234567890}, I want 1,234,567,890 in the result, how this can be done with jqecho '{"a": 1234567890}' | jq '.a | FORMAT?'Thanks for @peak's answer, the solution isecho '{"a": 1234567890}' |...
View Articlestumbled into jq last night
I have not yet understood the programming model.Some code works well - and easily - other stuff just confuses me.I have records that look like this{"items": [ {"id":...
View ArticleParse JSON string within JSON and other fields
I have a file in s3 which when unzipped has something of this format{"a": "foo","b": "bar","c": "{\"hello\": \"world\"}"}Now I know I can parse the value of c by doing jq '.c | fromjson | .hello'But...
View ArticleWhy is variable not expanding in jq?
Why does this work (variable expanded by bash) jq -r "include \"i3\";stack(.id==$ID)|.[]|select(.type==\"workspace\")|.name" <<< "$TREE"When this does not work (variable expanded by jq)jq -r...
View ArticleHow to convert a very nested JSON file (tree like), in CSV with same key at...
I have a PDF bookmark structure (tree like), which looks like this:Content Author 1 Piece 1 Piece 2 Author 2 Piece 3 Piece 4 Movement 1 Movement 2 Movement 3 Piece 5Using qpdf, it could be formatted in...
View Articlejq: Cannot index array with string
I have the following in a file (which I will call "myfile"):[{"id": 123,"name": "John","aux": [{"abc": "random","def": "I want this" }],"blah": 23.11}]I could parse it if the file did not have the...
View ArticleHow to pass JSON file name to jq?
Currently I callcat my_file.json | jqto pretty-print JSON data. I am a bit surprised that I can't provide a JSON file as a command line argument.I would like to avoid the extra cat; i.e.,jq...
View ArticleHow to check for presence of 'key' in jq before iterating over the values
I get Cannot iterate over null (null) from the below query because .property_history is not present in result object.How do I check for the presence of .property_history key before proceeding with...
View Article