I need to catch an error when lifting a service. The response can be null
, a string error message like
error services-migration/foobar: Not found: services-migration/foobar
or a valid JSON when everything is fine. I was wondering if there is a way with jq
to simply check if the provided string is a valid JSON. I could ofc check the string for some keywords like error
f.e., but I'm looking for a more robust option, where eg. I get a true/false
or 1/0
from jq.
I was looking through the docs of jq
and also some questions here on SO but everything was about finding and picking out key-values from a JSON, but nothing about simply validating a string.
UPDATE:
I've got this:
result=$(some command)
from which the result is the string error services-migration/foobar: Not found: services-migration/foobar
And then the if statement:
if jq -e . >/dev/null 2>&1 <<<"$result"; then
echo "it catches it"
else
echo "it doesn't catch it"
fi
And it always ends up in the else
clause.
question from:
https://stackoverflow.com/questions/46954692/check-if-string-is-a-valid-json-with-jq 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…