Issue
I'm using JQ to print out the output I get from a curl request.
Now, while the bash script works fine, I can't seem to be able to get JQ to print multiple returns in one line.
For example, if I do this:
var1 var2 var3 var4 < <(echo $(curl -s 'https://example.com/api/json/APIKEY/'$ip' | jq ".proxy,.vpn,.ISP,.timezone"))
I do get the results. But, if ISP's value is "Google Severs", the output would be
var3: Google
var4: Servers
Thanks for any help you can give me.
I've tried leaving space between the results, to see if that's the issue. But not much else, to be honest.
This is the actual result from CLI command:
curl -s (...) | jq ".proxy,.vpn,.ISP,.timezone" true true "Google Servers" "America/Los_Angeles"
This is the actual result from the script, using the same code: ./script.sh
curl -s (...) | jq ".proxy,.vpn,.ISP,.timezone"
true
true
"Google
Servers"
Solution
You can make jq print items in one line, but it's much more sensible (and makes you more robust to things like ISP names with spaces) to configure read
to read things that have newlines between them.
IFS=$'\n' read -r -d '' var1 var2 var3 var4 _ < <(
curl -s "https://example.com/api/json/APIKEY/$ip" |
jq -r '.proxy, .vpn, .ISP, .timezone, "\u0000"'
)
One can also include four different read
s, all reading the output of just one copy of jq
:
{ read -r var1 && read -r var2 && read -r var3 && read -r var4; } < <(
curl -s "https://example.com/api/json/APIKEY/$ip" |
jq -r '.proxy, .vpn, .ISP, .timezone'
)
...which can in turn be extended into a more robust approach that NUL-terminates each and every item, so they work properly even when the data items being extracted contain newlines:
{ for v in var{1,2,3,4}; do IFS= read -r -d '' "$v"; done; } < <(
curl -s "https://example.com/api/json/APIKEY/$ip" |
jq -r '(.proxy, .vpn, .ISP, .timezone) | (., "\u0000")'
)
Answered By - Charles Duffy Answer Checked By - Gilberto Lyons (WPSolving Admin)