Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if string is a valid JSON with jq

Tags:

json

bash

jq

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.

like image 839
Milkncookiez Avatar asked Oct 26 '17 12:10

Milkncookiez


People also ask

Can jq validate JSON?

jq – a lightweight and flexible CLI processor – can be used as a standalone tool to parse and validate JSON data.

How do I check if a JSON string is valid?

To check if a string is JSON in JavaScript, we can use the JSON. parse method within a try-catch block. to check if jsonStr is a valid JSON string. Since we created the JSON string by calling JSON.

What is a valid JSON string?

JSON can actually take the form of any data type that is valid for inclusion inside JSON, not just arrays or objects. So for example, a single string or number would be valid JSON. Unlike in JavaScript code in which object properties may be unquoted, in JSON only quoted strings may be used as properties.

Is valid JSON JavaScript?

You cannot test whether a string is valid JSON with a regex in JavaScript, since JS regexes don't support the necessary extensions (recursive regexes) that let you do so. Your above code fails on "{". @Mic json2. js no longer uses that simple check (instead uses a 4 stage parsing to determine valid JSON).


2 Answers

From the manual:

-e / --exit-status:

Sets the exit status of jq to 0 if the last output values was neither false nor null, 1 if the last output value was either false or null, or 4 if no valid result was ever produced. Normally jq exits with 2 if there was any usage problem or system error, 3 if there was a jq program compile error, or 0 if the jq program ran.

So you can use:

if jq -e . >/dev/null 2>&1 <<<"$json_string"; then     echo "Parsed JSON successfully and got something other than false/null" else     echo "Failed to parse JSON, or got false/null" fi 

In fact, if you don't care about distinguishing between the different types of error, then you can just lose the -e switch. In this case, anything considered to be valid JSON (including false/null) will be parsed successfully by the filter . and the program will terminate successfully, so the if branch will be followed.

like image 99
Tom Fenech Avatar answered Sep 21 '22 17:09

Tom Fenech


This is working for me

echo $json_string | jq -e . >/dev/null 2>&1  | echo ${PIPESTATUS[1]} 

that returns return code:

  • 0 - Success
  • 1 - Failed
  • 4 - Invalid

Then you can evaluate the return code by further code.

like image 24
Reddy SK Avatar answered Sep 25 '22 17:09

Reddy SK