I have a function like:
getServers() {
curl -s -X GET ...
}
The output is a complex JSON string. I can parse the JSON like this:
serverId=$(getServers | jq -r ".[] | select(whatever...)")
echo $serverId
But if I store the output of the function call in a variable, I get an error:
servers=$(getServers)
echo $servers | jq .
# jq can not process this
# parse error: Invalid string: control characters from U+0000 through
echo "$servers" | jq .
# does not work either
U+001F must be escaped at line ...
Even though I $servers
contain the same value as function call, jq
fails to process it. What's happening here?
$ jq --version
jq-1.5
Possible Answer
Pipe the string into tr '\r\n' ' '
:
servers=$(getServers)
echo $servers | tr '\r\n' ' ' | jq .
No comments:
Post a Comment