How to read a basic json array into a bash array [duplicate] - json

This question already has an answer here:
JSON list (not object) to Bash array?
(1 answer)
Closed 2 years ago.
I have an json formatted array as follows:
myArray='["GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN","GREEN"]'
How do I read this simple json array into a bash array? I have seen plenty of specific answers for specific problems on this but haven't been able to find an answer for the basic version of it.
What I want is an array a=(GREEN GREEN GREEN ...)
I have tried readarray -t array <<< $myArray. This strips the quotes but doesn't create an actual array.

It looks like you want a regular array, not an associative one. Just use jq to turn the JSON into a form suitable for use with the mapfile/readarray builtin:
#!/usr/bin/env bash
myArray='["GREEN","GREEN","GREEN","GREEN","GREEN"]'
readarray -t a <<< "$(jq -r '.[]' <<< "$myArray")"
echo "a has ${#a[#]} elements. First one is ${a[0]}."
will output
a has 5 elements. First one is GREEN.
Note that this won't work right if any of the array elements have newlines in them.

Related

why get all results of jq in a single array element in bash [duplicate]

This question already has answers here:
How do I assign the output of a command into an array?
(3 answers)
Closed 2 years ago.
I'm trying to get all results of jq query in a array in bash, but it gives me all results in the first array element.
Could you help me? I would like to having every result in one array element
bash-4.2$ try=$(tm run jobs:status::get -s "tm=serverA&work=*&status=Failed" | $PATH_API/jq -r '.statuses[].name // empty')
bash-4.2$ echo $try
job_B job_C
bash-4.2$ echo "${#try[#]}"
1
bash-4.2$
If the status names are sufficiently uncomplicated, you could add an extra pair of parentheses:
try=($(tm run ....))
(Consider also invoking jq without the -r option.)
Otherwise, you could consider using readarray, or other techniques for initializing bash arrays.

Why bash does not json split array with jq -r bash IFS explicitly defined variable [duplicate]

This question already has answers here:
Convert a JSON array to a bash array of strings
(4 answers)
Closed 2 years ago.
I am reading an array from a file
file.json
{
"content": ["string with spaces", "another string with spaces", "yet another string with spaces"]
}
#!/bin/bash
GROUP_ID_TEMP=( $(IFS=','; jq -r '.content' file.json) )
why does jq read content print or echo content as space separated array for the below codeblock at the whitespace rather than the comma ',' as explicitly stated?
for each in "${GROUP_ID_TEMP[#]}"
do
echo "$each" >> "file.txt"
done
Here's an easier way to reproduce your problem:
var=( $(IFS=','; echo "foo,bar" ) )
declare -p var
What you expect is declare -a var=([0]="foo" [1]="bar") but what you get is declare -a var=([0]="foo,bar")
This happens because IFS affects word splitting and not program output. Since there is no word splitting in the scope of your variable, it doesn't affect anything.
If you instead define it in the scope that does word splitting, i.e. the scope in which the $(..) expansion happens:
IFS=','
var=( $(echo "foo,bar") )
then you get the result you expect.

How to print a part of a JSON string and store it in variable? [duplicate]

This question already has answers here:
Parsing JSON with Unix tools
(45 answers)
Bash script store command output into variable
(2 answers)
Closed 3 years ago.
I am using curl to create a session to log into the switch.
Below the script that i use
curl --noproxy 10.23.193.1 -X POST http://10.23.193.1:80/rest/v3/login-sessions -d '{"userName":"admin", "password":"password"}'
After this is executed I get the following output
{"uri":"/login-sessions","cookie":"sessionId=DfZNCFbfoc3LDuMgjLXRiS8ZmEo4MWENCOJM0Iu14R1uMT9kKqbe1Rx6AedmoeT"}
My requirement here is to first only print this part of the string "sessionId=DfZNCFbfoc3LDuMgjLXRiS8ZmEo4MWENCOJM0Iu14R1uMT9kKqbe1Rx6AedmoeT"
Secondly I would want to know how to store the above string in a variable so that I could call the same variable for subsequent operations
I ran the following, but I am not getting any output.
curl --noproxy 10.23.193.1 -X POST http://10.23.193.1:80/rest/v3/login-sessions -d '{"userName":"admin", "password":"password"}' | grep -`E ""cookie":"
Avoid using simple tools like grep or sed to parse JSON as they won't handle things like quotes or multi-line data correctly. It's best to use a JSON-aware program such as jq.
With jq it's simple and robust:
curl ... | jq '.cookie'
To store the cookie in a variable use the -r flag to have JQ print out the raw, unquoted string.
cookie=$(curl ... | jq -r '.cookie')
Further reading:
jq Manual
Parsing JSON with Unix tools
How to parse JSON with shell scripting in Linux?

To be assigned value as variable in jq [duplicate]

This question already has answers here:
Passing bash variable to jq
(10 answers)
Closed 5 years ago.
json processor jq for a package.json
{
"someparent" : {
"somechild" : "oldvalue"
}
}
If I run the following command (to change oldvalue to somevalue):
jq '.someparent["somechild"] = "somevalue" "$aDirection/package.json"'
It is successfully changed. However, if I give a variable instead of someValue:
aVar="anotherValue"
jq '.someparent["somechild"] = "$aVar" "$aDirection/package.json"'
It is not working.
What I already tried:
["aVar"] #interpreted as string
["$aVar"] #interpreted as string
"$aVar" # aVar is not defined
$avar #aVar is not defined
The single quotes around the jq filter are in the wrong place. In your attempt made it is also enclosing the actual JSON file input. The right syntax when applying filter over files is
jq '<filter>' json-file
In your case the right filter is just
.someparent["somechild"] = $aVar
and to use a shell variable in to the jq, you need to use the --arg field. Putting together both of the options. The first variable after --arg is used in the context of jq and the one following it is the actual shell variable.
aVar="anotherValue"
jq --arg aVar "$aVar" '.someparent["somechild"] = $aVar' "$aDirection/package.json"
# ^^^^ Remember the single quote
# terminating here and not extended
Just to be clear: using the OP's approach can be made to work, e.g.
jq '.someparent["somechild"] = "'"$aVar"'"' "$aDirection/package.json"
but using --arg (for strings) and/or --argjson is far better. An additional option for environment variables is to use the jq function env.

How to grep the specific field value from json string using shell script [duplicate]

This question already has answers here:
Parsing JSON with Unix tools
(45 answers)
Closed 6 years ago.
Below is my JSON string available in file, out of which I need to extract the value for status in shell script.
Expected Output: status=success
response.json
{"eventDate":null,"dateProccessed":null,"fileName":null,"status":"success"}
Please let me know, if you need any information
Look into jq. It is a very handy json parser.
If you want to extract just the status you can do something like:
jq -r '.status' response.json
# output
success
You can format your output as well to follow the results you want.
jq -r '"status=\(.status)"' response.json
# output
status=success
You can try sed:
sed -n 's|.*"status":"\([^"]*\)".*|status=\1|p' response.json