Tech Tip: Checking malformed JSON in text
PRODUCT: 4D | VERSION: 16 | PLATFORM: Mac & Win
Published On: May 25, 2017
Here is a utlity method to check if a C_OBJECT that is in JSON text is malformed:
// -------------------------------------------------------------------------------- // Name: CHECK_MALFORMED_JSON // Description: Method will check if a JSON object construction is malformed. // // Input: // $1 (TEXT) - JSON object in text // // Output: // $0 (BOOLEAN) - False - Not malformed, True - Is malformed // -------------------------------------------------------------------------------- C_TEXT($1;$json) C_BOOLEAN($0;$result) C_OBJECT($obj) if (Count parameters=1) $json:=$1 ON ERR CALL("CHECK_MALFORMED_JSON") $obj:=JSON Parse($json) If (OB Is empty($obj)=True) $0:=true Else $0:=false End if End if |
Here is an example of normal JSON:

C_TEXT($json) $json:="{\"test\":\"123\"}" $status:=CHECK_MALFORMED_JSON ($json) // $status = False |
Here is an example of a malformed JSON:

C_TEXT($json) $json:="{\"test\":\"123}" $status:=CHECK_MALFORMED_JSON ($json) // $status = True |