JSONEncode

(Engine-Level Function)

Description: Converts any given VTScada value into a JSON string using UTF-8 encoding.
Returns: Text or Stream
Usage: Script only.
Function Groups: String and Buffer
Related to: JSONParse | Pack
Format: JSONEncode(InputValue[, PrettyPrint, Alphabetical, Indent, StreamVar])
Parameters:  
InputValue

Required. Any value to be encoded as JSON.

Text is expected to use UTF-8 encoding.

PrettyPrint
Optional Boolean. If TRUE, the output will have white space and line breaks added for presentation. Defaults to FALSE.
Alphabetical
Optional Boolean. If TRUE, dictionaries will be listed alphabetically. If FALSE, dictionaries will be listed chronologically. Defaults to FALSE.
Indent
Optional numeric. Ignored if PrettyPrint is FALSE. Sets the number of spaces to indent the encoding when PrettyPrint is TRUE.
StreamVar
Optional. If a valid stream is provided, the JSON encoded string will be appended to that stream and returned. The default is for JSONEncode to return a text string.
Comments:

VTScada value types that do not have a string or numeric representation, and are not dictionaries, arrays or structures, will be represented as JSON null.

Arrays of up to three dimensions are supported.

Example:

<
{============================== JSONEncodeSample ===========================}
{ Example code to demonstrate JSONEncode().                                 }
{===========================================================================}
JSONEncodeSample
[
  Protected Input               { Input dictionary                          };
  Protected JSONResult          { Input encoded as JSON                     };
  Protected PrettyPrintedResult { Pretty printed JSON result                };
]

Main [
  If Watch(1);
  [
    Input = Dictionary();
    Input["Values"   ] = New(3);
    Input["Values"   ][0] = 10;
    Input["Values"   ][1] = 20;
    Input["Values"   ][2] = 30;
    Input["Timestamp"]    = "2017-01-02T03:24:19Z";

    JSONResult = JSONEncode(Input);
    PrettyPrintedResult = JSONEncode(Input, TRUE);
  ]
]

{ End of JSON_EncodeSample }
>

JSONResult will be the following:

{"Values":[10,20,30],"Timestamp":"2017-01-02T03:24:19Z"}

PrettyPrintedResult will be the following:

{
  "Values": [
    10,
    20,
    30
   ],
  "Timestamp": "2017-01-02T03:24:19Z"
}