1

I am new to jq. I am trying to write a simple script that loops through a JSON file, gets two values within each object and assigns them to two separate variables I can use with a curl REST call. I see both values as output when I echo $i but how can I get value and addr as separate variables?

for i in `cat /Users/egraham/Downloads/test2  | jq .[] | jq ."value,.addr"`; do
5
  • Could you please provide full shell code (seems to be cut after do) and the JSON file or at least a short sample? Commented Jan 31, 2017 at 20:19
  • for i in cat /Users/egraham/Downloads/test2 | jq .[] | jq ."value,.addr"; do echo $i done Commented Jan 31, 2017 at 20:22
  • And the JSON file? Commented Jan 31, 2017 at 20:22
  • { "populator": { "dimension_id": "221", "value": “EDGE2-SYD2", "direction": "dst", "addr": “192.168.3.0/24" } } { "populator": { "dimension_id": "221", "value": “EDGE2-BOS5", "direction": "dst", "addr": “192.168.1.0/24" } } Commented Jan 31, 2017 at 20:24
  • I would like to get value and addr as seperate variables so I can pass them to curl. The JSON file includes 1000s of lines with this format. I see the right output with echo $i Commented Jan 31, 2017 at 20:26

3 Answers 3

5

You can do this:

jq -rc '.populator.value + " " + .populator.addr' file.json |
while read -r value addr; do
    echo do something with "$value" and "$addr"
done
Sign up to request clarification or add additional context in comments.

Comments

2

If spaces or tabs or other special characters make using 'read -r' problematic, and if your shell has "readarray", then it could be used:

$ readarray -t v < <(jq -rc '.populator | (.value,.addr)' file.json)

The values would then be available as ${v[0]} and ${v[1]}

This approach is especially useful if there are more than two values of interest, or if the number of values is variable or not known beforehand.

If your shell does not have readarray, then you can still use the array-oriented approach, e.g. along the lines of:

i=-1; while read -r a ; do i=$((i+1)); v[$i]="$a" ; done

Comments

-1

First:

for i in cat /Users/egraham/Downloads/test2 | jq .[] | jq .value; do echo $i done

Second:

for i in cat /Users/egraham/Downloads/test2 | jq .[] | jq .addr; do echo $i done

I don't know any way to get it without running the commands separately. I don't know AWK, but maybe it's something worth considering.

4 Comments

Thanks... but I need one curl statement and include both variables like this
curl -v https:///customdimension/221/populator -X POST -d '{"populator": {"dimension_id":"221","value":'$i',"direction":"src","addr":"$j"}}'
where '$i' represents value and '$j' represents addr
The two invocations of jq in 'jq .[] | jq .value' can and should be reduced to one, as in: jq '.[] | .value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.