JSONParse

Parses text as JSON data and keeps the parsed JSON representation in memory for you to reuse with other JSON functions.

Format 

JSONParse ( json )

Parameters 

json - any text expression or field that contains a JSON object or array.

Data type returned 

text

Originated in version 

22.0

Description 

JSONParse first evaluates the input json text expression, then parses the JSON text and caches a binary representation in memory. When JSONParse sets a variable, script parameter, or is used in other calculations, this binary representation is available for other JSON functions to process the parsed data.

JSONParse returns the original text unchanged if json is valid, or returns an error message if invalid. For invalid JSON, JSONParse returns an error message that begins with "?" followed by details about the parsing error, similar to other JSON functions.

See Optimizing JSON performance.

Notes 

  • For single JSON operations, using other JSON functions directly on the text may perform as well as using JSONParse first.

  • You can use the JSONParsedState function to verify whether JSON data has been successfully parsed and is valid.

Example 1 - Basic parsing of a simple JSON object

JSONParse ( "{ \"name\": \"Alea\", \"age\": 30, \"city\": \"New York\" }" ) returns { "name": "Alea", "age": 30, "city": "New York" }, which is the unchanged text representation of the JSON data.

If set to a variable $$Contact, the binary representation is cached in memory and available to calculations that use $$Contact.

Copy
Let ( [
  $$Contact = JSONParse ( "{ \"name\": \"Alea\", \"age\": 30, \"city\": \"New York\" }" )
  ] ;
  $$Contact
)

If the JSON is invalid, JSONParse parses the data but returns an error message. For example, if the first comma is missing in the JSON above, JSONParse returns:

Copy
? * Line 1, Column 18
  Missing ',' or '}' in object declaration

Example 2 - Parsing multiple JSON objects

Uses JSONParse to parse JSON text in $JSONText1 and $JSONText2, then retrieves elements from the variables $ParsedJSON1 and $ParsedJSON2 without causing the JSON data to be parsed again.

Copy
Set Variable [ $ParsedJSON1 ; Value: JSONParse ( $JSONText1 ) ]
Set Variable [ $ParsedJSON2 ; Value: JSONParse ( $JSONText2 ) ]
Set Variable [ $name ; Value: JSONGetElement ( $ParsedJSON1 ; "name" ) ]
Set Variable [ $id ; Value: JSONGetElement ( $ParsedJSON2 ; "id" ) ]
Set Variable [ $category ; Value: JSONGetElement ( $ParsedJSON1 ; "category" ) ]

Example 3 - Error checking and using parsed JSON in a loop

Demonstrates error checking and shows how to use JSONParse to improve performance when processing multiple elements from the same JSON data in a loop. $$JSON contains the JSON text in the Example JSON data. After the script parses $$JSON, it checks whether the parsed JSON is valid (when the JSONParsedState function returns a positive value). If the parsed JSON is valid, the script loops through the bakery.product array and gets values from the already parsed JSON data.

Copy
Set Variable [ $ParsedJSON ; Value: JSONParse ( $$JSON ) ]
If [ JSONParsedState ( $ParsedJSON ) > 0 ]
   # JSON is valid. Use the parsed JSON for multiple operations.
   Set Variable [ $ProductCount ; Value: ValueCount ( JSONListKeys ( $ParsedJSON ; "bakery.product" ) ) ]
   Set Variable [ $i ; Value: 0 ]
   Loop [ Flush: Always ]
      Set Variable [ $Product ; Value: JSONGetElement ( $ParsedJSON ; "bakery.product[" & $i & "]" ) ]
      Set Variable [ $ProductName ; Value: JSONGetElement ( $Product ; "name" ) ]
      Set Variable [ $ProductPrice ; Value: JSONGetElement ( $Product ; "price" ) ]
      # Process product data...
      Set Variable [ $i ; Value: $i + 1 ]
      Exit Loop If [ $i ≥ $ProductCount ]
   End Loop
Else
   # Handle JSON parsing error
   Show Custom Dialog [ "JSON Error" ; $ParsedJSON ]
End If