Tuesday 19 June 2018

shell - Invalid string: control characters from U+0000 through U+001F must be escaped using Bash?

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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...