JSONParsedState
Returns 0 if JSON is not parsed, -1 if parsed but invalid, or a positive number representing the JSON type if parsed and valid.
Format
JSONParsedState ( json )
Parameters
json
- any text expression or field that contains a JSON object or array.
Data type returned
number
Originated in version
22.0
Description
JSONParsedState indicates whether the specified JSON has been parsed (and has a binary representation in memory), and whether the JSON is valid or invalid. This function returns:
-
0 if the JSON has not been parsed (no binary representation exists in memory).
-
-1 if the JSON was parsed but is invalid. If this JSON is used with the other JSON functions, they return "?" followed by an error message.
-
1-6 if the JSON is valid and parsed. The value corresponds to one of the JSON types defined in JSONSetElement.
To explicitly parse JSON text, use the JSONParse function.
Example 1
-
JSONParse ( "[3]" )
returns [3] because JSONParse returns the input JSON as text if it's valid JSON. -
Let ( $a = JSONParse ("[3") ; JSONParsedState ($a) )
returns -1 because $a was set to a parsed but invalid JSON array. -
Let ( $a = "[3]" ; JSONParsedState ($a) )
returns 0 because $a wasn't parsed. -
JSONParsedState ( JSONParse (Pi) )
returns -1 because the value Pi was parsed but isn't valid JSON. -
Let ( $a = JSONSetElement ( "" ; "a" ; "b" ; JSONString ) ; JSONParsedState ($a) )
returns 3 because JSONSetElement created a parsed JSON object stored in $a. -
Let ( $a = JSONParse ("[3]") ; JSONParsedState ($a) )
returns 4 because $a was parsed and contains a JSON array. -
JSONParsedState ( JSONParse ("") )
returns -1 because an empty string was parsed, which is invalid JSON.
Example 2
Let ( [
a = "[3"; /* String is an invalid JSON array */
b = JSONParse ( a ) ; /* Error: Input is invalid JSON */
c = JSONParsedState ( a ) ; /* 0: 'a' is still only text (not parsed) */
d = JSONParsedState ( b ) ; /* -1: 'b' has been parsed but is invalid */
e = JSONFormatElements ( b ) ; /* Error: 'b' is invalid */
f = JSONFormatElements ( a ) /* Error: JSON text in 'a' is invalid */
] ;
a &¶& b &¶& c &¶& d &¶& e &¶& f
)
Returns:
[3
? * Line 1, Column 3
Missing ',' or ']' in array declaration
0
-1
? * Line 1, Column 3
Missing ',' or ']' in array declaration
? * Line 1, Column 3
Missing ',' or ']' in array declaration
In contrast, if variable a
is a valid JSON array ("[3]"), this example returns:
[3]
[3]
0
4
[ 3 ]
[ 3 ]
Note these differences:
-
Line 2:
JSONParse ( a )
returns the original text passed in to JSONParse. -
Line 4:
JSONParsedState ( b )
reflects that variableb
has been parsed and is a valid JSON array. -
Lines 5 and 6: JSONFormatElements returns formatted text for both the parsed JSON in
b
and the text JSON ina
.