I'm trying to use an API with curl, returning a JSON:
response=$(curl -i --user api:$APIKey --data-binary @$target https://api.tinypng.com/shrink)
Next, I try to parse (briefly) the response with a function:
parseJson(){
result="$1"
result=($(echo $result | python -mjson.tool))
result=${result%\"*}
result=${result##*\"}
return $result
}
and I'm calling it like this: message=$(parseJson "$response" message)
. FYI, the response is on multiple lines.
But weird thing happened: python gave me No JSON object could be decoded
, but if I echoed $result there is a good JSON string. Weirder, if I echoed it before calling python, it looks like python is executed first anyway.
Is there some asynchronous trick ? Why can't I pass my variable string to python ?
Any help or better method will be greatly appreciated!