jq

How to use jq to get a value from two possible paths?

How to use jq to get a value from two possible paths? I wanted to use jq to get a specific value b from two possible paths (and these two only). So, basically, I wanted to say jq ‘(path1 OR path2) | .b’ which should work if b is at one of the two paths …

Total answers: 1

Add t to jq resulted array extraction

Add t to jq resulted array extraction I want to add t before each element in the resulting jq array and this is for formatting purposes as the result will get added to a file. here is the json file: { "array": ["element1", "element2"] } When you read this array with jq content=$(jq ‘.array’ file) …

Total answers: 1

jq skip iteration over null

jq skip iteration over null I am using jq 1.7 Data I am operating on info.json downloaded by yt-dlp yt-dlp –write-info-json –skip-download https://www.youtube.com/watch?v=vlIO-7Rpi7c JQ – Task Now I want to remove all array inside subtitles whose name is not de, en-US and inside de, en-US select all objects with element ext == vtt. This successfully …

Total answers: 1

jq: Printing multiple values from multiple arrays at once

jq: Printing multiple values from multiple arrays at once The default functionality of jq is to send each object from an array one at a time, though the join operator can merge those values. My problem is in trying to print all the values from multiple arrays at once. Taking this example: { "key1": { …

Total answers: 2

JQ returns json lines without commas

JQ returns json lines without commas To be clear – I want a pseudo-JSON output that will be written into another static JSON file here… so not wrapped in an array or anything, simply getting the missing commas after each entity in my output. Currently my query is: .[] | select(.auditId == "categories") | { …

Total answers: 2

Is there a simple way to store bash variables via jq?

Is there a simple way to store bash variables via jq? I am seeking an easy way to create persistent bash associative array (dictionary) storage without using declare and its options to manually read and write associative arrays externally. ‘jq’ and ‘json’ storage is certainly more capable than internal bash storage, but a simple answer …

Total answers: 1

jq expression to merge

jq expression to merge I have the below JSON document from the Junos REST API. I am trying to simplify the document down to just a select few items. How would I accomplish this? Expected JSON: [ { "status": .member-status[0].data, "serial": .member-serial-number[0].data, "slot": .fpc-slot[0].data }, … ] Original JSON: { "member-status": [ { "data": "Prsnt" …

Total answers: 1

Search field and replace other in jq

Search field and replace other in jq I am trying to search for an object having a specific property and then update another property. Given this input: [ { "replacements": [ { "newValue": "0", "yamlPath": "k8s-helm-templates.deployment.containers.abc.image.tag" }, { "newValue": "0", "yamlPath": "k8s-helm-templates.deployment.containers.def.image.tag" }, { "newValue": "0", "yamlPath": "k8s-helm-templates.deployment.containers.ghi.image.tag" } ], "yamlFilePath": "k8s/helm/Dev/us-east-1/values.yaml" } ] and …

Total answers: 1

How to pass a single value argument to a JSON file from a script without overriding the existing values?

How to pass a single value argument to a JSON file from a script without overriding the existing values? I have the following JSON file named pool.json: { "AllocateActions": {}, "Available": true, "Description": "Pool for nodes in cluster – {{CLUSTER_NAME}}", "Endpoint": "", "EnterActions": { "AddProfiles": [ "{{RC_JOIN_PROFILE}}", "image-deploy-profile", "rc-controlplane-profile", "rc-etcd-profile" ], "Workflow": "rc-image-deploy" }, "Errors": …

Total answers: 1

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

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 …

Total answers: 1

Add/modify key/value in json object

Add/modify key/value in json object I’ve asked a similar question in the past but I’m having a hard time adapting that solution to this problem. I have the following json array: $ jq <<<"$json" [ { "id": "node1" }, { "id": "node2" }, { "id": "node3" } ] I want to add a key/value to …

Total answers: 1

Don't split at spaces in jq output

Don't split at spaces in jq output I used jq to convert an array of words into an argument for a java program. its like arguments=$(jq ‘.nameField | "-i " + """ + . + """’ file.json) java …. $arguments <– No quotes wrapped around arguments. Expected output: Java must read as -i "Name 1" …

Total answers: 3

Convert csv out of json in new coloumn

Convert csv out of json in new coloumn The below is my json code [ { "kind": "user", "version": "v2", "metadata": { "name": "person1" }, "spec": { "roles": [ "access", "auditor", "editor", "abc-access" ], "status": { "is_locked": false, "locked_time": "0001-01-01T00:00:00Z", "lock_expires": "0001-01-01T00:00:00Z", "recovery_attempt_lock_expires": "0001-01-01T00:00:00Z" }, "expires": "0001-01-01T00:00:00Z", "created_by": { "time": "2023-06-07T08:50:58.559675944Z", "user": { "name": "person2" …

Total answers: 1

Convert json to csv with headers in jq

Convert json to csv with headers in jq Is it possible to convert this json: [ { "bytes": 276697, "checked": false }, { "bytes": 276697, "checked": false } ] to a table WITH headers in jq? I’ve tried: cat file.json | jq ‘.[] | join(",")’ but it omits headers: "276697,false" "276697,false" it should be: "bytes,checked" …

Total answers: 2

jq not showing all the results

jq not showing all the results I have the following json: [ { "_source": { "layers": { "http2": { "http2.stream": { "http2.length": "1030" } }, "http2": { "http2.stream": { "http2.length": "1246" } } } } } ] I am doing the following jq command: jq ‘.[]._source.layers.http2."http2.stream"’ file.json I am expecting to get the following result: …

Total answers: 1

How to format a JSON string as a table using jq?

How to format a JSON string as a table using jq? I need to convert a JSON string using jq, as shown below, into a table format for displaying the output in the terminal. { "results": [ [ { "field": "@timestamp", "value": "2023-07-03 08:28:00.000" }, { "field": "CpuReserved", "value": "8192.0" }, { "field": "CpuUtilized", "value": …

Total answers: 4

Using jq to parse invalid JSON?

Using jq to parse invalid JSON? Can jq parse invalid JSON such as this?: [2023.06.07-21.58.47] StatManagerLog: { "RoundState": { "State": "Starting", "Timestamp": "2023.06.07-21.58.47" } } [2023.06.07-21.58.47] StatManagerLog: { "RoundState": { "State": "StandBy", "Timestamp": "2023.06.07-21.58.47" } } Ideally each StatManagerLog object would be read by jq. Cannot seem to find out how jq can do this. …

Total answers: 2

Add new key to JSON file, with value equal to the length of other key's value

Add new key to JSON file, with value equal to the length of other key's value I’m adjusting a JSON file that holds a large number of objects. Here are the first three objects as an example: {"id":"0704.0001","submitter":"Pavel Nadolsky","authors":"C. Bal\’azs, E. L. Berger, P. M. Nadolsky, C.-P. Yuan","title":"Calculation of prompt diphoton production cross sections at …

Total answers: 2

How to keep one specific key from a nested json object?

How to keep one specific key from a nested json object? I have the following config.json file: { "auths": { "reg1.io": { "auth": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" }, "reg2.io": { "auth": "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" }, "reg3.io": { "auth": "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" } } } I want to keep only the reg2.io object to get: { "auths": { "reg2.io": { "auth": "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" } …

Total answers: 1