JSONParse

(Engine-Level Function)

Description: Parse JSON text, converting it to VTScada arrays, objects / structures, strings or other data types as appropriate.
Returns: Varies. Invalid on non-recoverable error.
Usage: Script Only.
Function Groups: String and Buffer
Related to: JSON_Encode | XMLParse
Format: JSONParse(JSONInputText[, ErrorMessageOut, ObjectsAsStructures])
Parameters:  
JSONInputText
Required. JSON text to be parsed.
ErrorMessageOut
Optional text. Output parameter that is set to a text representation of the error.
If recoverable parsing errors occur, this may be set in addition to the return value being valid. If for example an array of strings has one string that cannot be displayed in the system's configured code page, only that element of the VTScada array will be invalid and the ErrorMessageOut text will reflect this.
ObjectsAsStructures
Optional Boolean. If this parameter is TRUE, JSON objects will be created as VTScada structures for compatibility with XMLParse(). If this is not specified or is false, JSON objects will be translated into VTScada dictionaries. Defaults to FALSE.
Comments:

Dictionaries and structures returned by JSONParse() are case sensitive. The order that JSON object names are inserted into VTScada dictionaries or structures should be treated as undefined.

The input text is expected to be encoded as UTF-8, as per the JSON standard. Text using another encoding can be converted to UTF-8 with TextEncode().

Example:

<
{================================= JSONTest ================================}
{ Example code to demonstrate JSONParse().                                  }
{===========================================================================}
JSONTest
[
  Protected JSONInput = "{""type"": ""temperature"", ""values"" : 
                       [23.5, 19.7, 21.4], ""enabled"": true}";
  Protected Result;
]

Main [
  If Watch(1);
  [
    Result = JSONParse(JSONInput);
  ]
]

{ End of JSONTest }
>

In this example Result would be a dictionary containing three keys: "type", "values" and "enabled". The "type" key's value is a string "temperature". "Value" is an array of 23.5, 19.7 and 21.4, and "enabled" is the value 1.