How to pass bash variable as a key to jq? [duplicate] - bash

This question already has answers here:
Passing bash variable to jq
(10 answers)
"Invalid numeric literal" error from jq trying to modify JSON with variable
(1 answer)
Closed 4 years ago.
Here is the example json
{
"app": "K8s",
"version": "1.8",
"date": "2018-10-10"
}
In order to get the value of app, I can do this in jq as
jq '.app'
But what I want is, I want to pass the key to jq as a bash variable, i.e
bash_var="app"
jq '."${bash_var}"'
I'm getting the output as null instead of the value. What is the correct syntax to achieve this?

First, you need to port the bash variable into jq's context usign the --arg flag and access it inside the [..]
jq --arg keyvar "$bash_var" '.[$keyvar]' json

Related

JQ output not correctly assinging to variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 9 months ago.
Below jq query output comes correctly.
ROUTE_ID= jq -r '.[][]? | select(.pattern? == "*test.com/testcards/email/*").id' route.json
route.json file contains a json output.
But echo "this is route $ROUTE_ID" or echo "this is route $ROUTE_ID does not return value for $ROUTE_ID"
What you are currently doing is setting the environment variable ROUTE_ID to nothing for the execution of jq, eg:
MY_ENV=abc command
Will set the environment variable "MY_ENV" to "abc" for the execution of command.
What you want to do is store the output of your command a variable, for this you'll need to use command substitutions:
my_var=$(command)
In your case:
route_id=$(jq -r '.[][]? | select(.pattern? == "test.com/testcards/email/").id' route.json)
Nitpicking; use lowercase variable names when possible, as UPPER_CASE are "reserved" for exported environment variables.

Append to a list in a JSON document from bash [duplicate]

This question already has answers here:
how to add json object to json file using shell script
(2 answers)
Closed 2 years ago.
I have a file static-nodes.json, which has the following content >>>
[
"enode://70399c3d1654c959a02b73acbdd4770109e39573a27a9b52bd391e5f79b91a42d8f2b9e982959402a97d2cbcb5656d778ba8661ec97909abc72e7bb04392ebd8#127.0.0.1:21000?discport=0&raftport=50000",
"enode://56e81550db3ccbfb5eb69c0cfe3f4a7135c931a1bae79ea69a1a1c6092cdcbea4c76a556c3af977756f95d8bf9d7b38ab50ae070da390d3abb3d7e773099c1a9#127.0.0.1:21001?discport=0&raftport=50001"
]
I want to add another enode >>>
"enode://56e81550db3ccbfb5eb69c0cfe3f4a7135c931a1bae79ea69a1a1c6092cdcbea4c76a556c3af977756f95d8bf9d7b38ab50ae070da390d3abb3d7e773099c1a9#127.0.0.1:21001?discport=0&raftport=50002"
But when I am trying to append it using the following command—>>>>>
Echo "enode to be appended " >> static-nodes.json
It is appended outside of the brackets as shown below
[
"enode://70399c3d1654c959a02b73acbdd4770109e39573a27a9b52bd391e5f79b91a42d8f2b9e982959402a97d2cbcb5656d778ba8661ec97909abc72e7bb04392ebd8#127.0.0.1:21000?discport=0&raftport=50000",
"enode://56e81550db3ccbfb5eb69c0cfe3f4a7135c931a1bae79ea69a1a1c6092cdcbea4c76a556c3af977756f95d8bf9d7b38ab50ae070da390d3abb3d7e773099c1a9#127.0.0.1:21001?discport=0&raftport=50001"
]
"enode://56e81550db3ccbfb5eb69c0cfe3f4a7135c931a1bae79ea69a1a1c6092cdcbea4c76a556c3af977756f95d8bf9d7b38ab50ae070da390d3abb3d7e773099c1a9#127.0.0.1:21001?discport=0&raftport=50001"
How should I fix this.?
Please have a look at
how to add json object to json file using shell script
what you can do it
Option 1:
create a temp json file using and then merge the temp json to nodes-json
toBeAppended = "enode to be appended"
echo "[ $toBeAppended ]" > tempNodes.json
jq -s add tempNodes.json static-nodes.json
Option 2. go with sed, awk, grep utilities

Extract a value from a nested json key in bash

Consider I have the below JSON:
{
"id": "Ab12",
"details": "{\"timeValue\":null,\"lastModifiedIn\":\"PHX\"}",
"version": 3
}
I want to extract the value of 'lastModifiedIn' key without using the jq command.
Basically, the result I seek for is 'PHX'.
Is there a way to extract this using basic shell scripting?
It's sloppy but given the input and your requirement to not use jq this might be good enough for your purposes:
$ sed -n 's/.*"lastModifiedIn\\":\\"\([^\\]*\).*/\1/p' file
PHX

Failure in extracting the contents of a json field [duplicate]

This question already has answers here:
jq not working on tag name with dashes and numbers
(2 answers)
Closed 4 years ago.
I have a (not so complicated) json file and I need to extract its contents using bash. I want to use jq for the processing, it should be straightforward. The problem is that I'm getting a weird error in the processing that I don't know how to solve (because I don't know what is causing it).
A minimal sample causing me problems:
{
"E23763": {
"data": "information"
}
}
If I just run jq to pretty-print it, it works:
$ cat test.json | jq .
{
"E23763": {
"data": "information"
}
}
But if I try to extract the first field, it fails criptically:
$ cat test.json | jq .E23763
jq: error: Invalid numeric literal at EOF at line 1, column 7 (while parsing '.E23763') at <top-level>, line 1:
.E23763
jq: 1 compile error
The expected result would had been:
{
"data": "information"
}
Anyone found a similar issue? Why it is complaining about a numeric literal when he is really looking into a string?
Quotation didn't seem to matter here, same error.
Please refer to this issue on GitHub there are many responses posted here which might help you with your problem: https://github.com/stedolan/jq/issues/1526
I'll post one of the solutions here however:
jq '.["E23763"]' test.json
Another Solution as said by #Inian is:
jq '."E23763"' json
Without the [], in this case it was the correct solution but try both nonetheless
Basically the parser is buggy and treats .E as the beginning of a number.

jq not working with key including dash [duplicate]

This question already has answers here:
jq not working on tag name with dashes and numbers
(2 answers)
Closed 5 years ago.
I have a REST API, which returns something like this:
{
"foo": 1,
"bar": 2,
"foo-bar": 3
}
when I do `http /endpoint/url | jq '.foo-bar', it gave the following error:
jq: error (at <stdin>:1): null (null) and boolean (true) cannot be subtracted
it looks like jq thinks I'm trying to do arithmetic operation with foo-bar.
How do I correctly form this kind of path? Or this is a bug of jq?
In JSON text, JSON keys are always double-quoted. Perhaps your REST API was formatting it properly in double-quotes and your example in your last edit was incorrect. Because without the same jq cannot parse the syntax as a valid JSON.
As far the issue you are seeing, you need to put the field within quotes to let jq know that it is a single field foo-bar you are accessing and not as separate fields
jq '."foo-bar"'
Or more specifically use the array access operator as jq '.["foo-bar"]'

Resources