This question already has answers here:
Exporting JSON to environment variables
(7 answers)
Closed 1 year ago.
I am trying to read the json file and store the keys as bash variable and the values as variable value. For instance here is my test.json:
{
"Day":'0',
"num":'85',
"Qtr":"[20183, 20184, 20191, 20192]",
"pSize":"75"
}
I need to store the variables like this in a bash file:
$Day=0
$num=85
$Qtr=[20183, 20184, 20191, 20192]
$psize=75
I found a way to extract the value using jq, but I am unable to store the key as variable in bash
-bash-4.2$ Qtr=`jq .Qtr test.json`
-bash-4.2$ echo $Qtr
"[20183, 20184, 20191, 20192]"
Could someone please provide a snippet as to how to loop through a json file and store the key as variables and values as values?
Thank you in advance
Would you please try the following:
#!/bin/bash
while IFS== read key value; do
printf -v "$key" "$value"
done < <(jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]' test.json)
# test of the variables
echo "Day: $day"
echo "Qtr: $Qtr"
echo "num: $num"
echo "pSize: $pSize"
The jq command jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]' test.json converts the json object into key=value pairs.
The output of jq is fed to the while loop via the process substitution
<(command).
The -v varname option to the printf command assigns the variable
indirecly expressed with varname to the output.
Related
I'm using groovy script and write this line
Version_List=$(jq -r '.items[] .version' "${projectContent}")
the ${projectContent} is another variable which is result of curl command
when i run the pipeline this is the error /bin/jq: Argument list too long
Use jq -Rs . <<< "${projectContent}" to convert the variable content into a JSON string (you may even use curl directly, like so: curl … | jq -Rs), then import that string into a jq variable using --slurpfile with Process Substitution
jq --slurpfile projectContent <(
jq -Rs . <<< "${projectContent}"
) '...' # your jq code here using variable $projectContent
Don't forget to also add either -n or an input file.
I have a JSON where I am storing it in a variable in my Bash script like this:
RAW_JSON="{"secretKey": "ADFGHJKGBNJK"}"
I wanted to store the value of secretKey in another variable called secretValue. How can I do that?
To preserve double quotation (") marks around the key and value, the syntax is:
$ RAW_JSON='{"secretKey": "ADFGHJKGBNJK"}'
$ echo "$RAW_JSON"
{"secretKey": "ADFGHJKGBNJK"}
Then jq can be used to return the value:
$ secretValue=$(echo "$RAW_JSON" | jq -r .secretKey)
$ echo "$secretValue"
ADFGHJKGBNJK
The -r parameter removes the quotes from the result.
I am trying to write a shell script that loops through a JSON file and does some logic based on every object's properties. The script was initially written for Windows but it does not work properly on a MacOS.
The initial code is as follows
documentsJson=""
jsonStrings=$(cat "$file" | jq -c '.[]')
while IFS= read -r document; do
# Get the properties from the docment (json string)
currentKey=$(echo "$document" | jq -r '.Key')
encrypted=$(echo "$document" | jq -r '.IsEncrypted')
# If not encrypted then don't do anything with it
if [[ $encrypted != true ]]; then
echoComment " Skipping '$currentKey' as it's not marked for encryption"
documentsJson+="$document,"
continue
fi
//some more code
done <<< $jsonStrings
When ran on a MacOs, the whole file is processed at once, so it does not loop through objects.
The closest I got to making it work - after trying a lot of suggestions - is as follows:
jq -r '.[]' "$file" | while read i; do
for config in $i ; do
currentKey=$(echo "$config" | jq -r '.Key')
echo "$currentKey"
done
done
The console result is parse error: Invalid numeric literal at line 1, column 6
I just cannot find a proper way of grabbing the JSON object and reading its properties.
JSON file example
[
{
"Key": "PdfMargins",
"Value": {
"Left":0,
"Right":0,
"Top":20,
"Bottom":15
}
},
{
"Key": "configUrl",
"Value": "someUrl",
"IsEncrypted": true
}
]
Thank you in advance!
Try putting the $jsonStrings in doublequotes: done <<< "$jsonStrings"
Otherwise the standard shell splitting applies on the variable expansion and you probably want to retain the line structure of the output of jq.
You could also use this in bash:
while IFS= read -r document; do
...
done < <(jq -c '.[]' < "$file")
That would save some resources. I am not sure about making this work on MacOS, though, so test this first.
I have a json object I am reading with jq and trying to write some properties with local variables.
I am setting local variable in my shell script like so:
LOCATION_NAME="stag5"
DOMAIN_LOCATION="example.io"
I am then building out the following variable:
echo "Build New ID"
DOMAIN_NAME_BUILT="$LOCATION_NAME.$DOMAIN_LOCATION.s3-website.us-east-2.amazonaws.com"
I am trying to read my distconfig.json file and set the properties with the above variables.
tmp=$(mktemp)
jq '.Origins.Items[0].DomainName = "$DOMAIN_NAME_BUILT"' distconfig.json > "$tmp" && mv "$tmp" distconfig.json
The command is working, but it is passing in the variable as a string to my new json file.
So when I view the property in the new created json file it is being saved as "$DOMAIN_NAME_BUILT" instead of stag5.example.io.s3-website.us-east-2.amazonaws.com
How can I instead of passing the string pass the variable for $DOMAIN_NAME_BUILT and write it to the new json file
Use the --argjson option instead of parameter interpolation.
jq --argjson dnb "$DOMAIN_NAME_BUILT" \
'.Origins.Items[0].DomainName = $dnb' distconfig.json > "$tmp" &&
mv "$tmp" distconfig.json
(Your immediate issue is that parameter expansion doesn't occur inside single quotes, but building a static filter that takes an argument is safer than building a filter dynamically.)
This question already has answers here:
Convert a nested JSON of objects into array into a bash array using jq
(2 answers)
Is there a way to read all key-value pairs in the JSON file and then initialize all variables accordingly in shell?
(1 answer)
Accessing a JSON object in Bash - associative array / list / another model
(7 answers)
Closed 5 years ago.
I have json string like this:
jstring='[{"userQuery":"select name from abc;","user":"abc"},{"userQuery":"select name from xyz;","user":"xyz"},{"userQuery":"select name from ppp;","user":"ppp"}]'
I wrote a simple for loop using jq to extract values but not getting the desired result.
for i in `echo $jstring | jq '.[] | [.user, .userQuery]'`; do echo ${i}; done
With the help of this line : echo $jstring | jq '.[] | [.user, .userQuery]'. I am able to extract below info:
[ "abc", "select name from abc;"][ "xyz", "select name from xyz;"][ "ppp", "select name from ppp;"]
Now, I want two variable "user" & "query" for each array and store that info.
Eg: For [ "abc", "select name from abc;"] -- user: abc, query: "select name from abc" and store them.
I am not sure how to iterate over json using jq and get individual values and store them.
I assume you mean you want to store them as shell variables. With bash you can write:
while read -r user query; do
echo "user->$user"
echo "query->$query"
done < <( jq -r '.[] | "\(.user) \(.userQuery)"' <<< "$jstring" )
user->abc
query->select name from abc;
user->xyz
query->select name from xyz;
user->ppp
query->select name from ppp;
The secret sauce is in the formulation of the string inside jq that refers to the object properties with \(.user)
I'm assuming that the user name does not contain spaces. Otherwise we have to use a different separator in that string, and use IFS with the shell read command.
jq + bash solution:
#!/bin/bash
jstring='[{"userQuery":"select name from abc;","user":"abc"},{"userQuery":"select name from xyz;","user":"xyz"},{"userQuery":"select name from ppp;","user":"ppp"}]'
while IFS=$'\t' read -r user query; do
msg="User: ${user} is running : ${query}"
mail -s "User query" "youremail#gmail.com" <<< "$msg"
done < <(jq '.[] | [.user,.userQuery] | #tsv' <<< "$jstring")