Json
- Last UpdatedDec 02, 2024
- 4 minute read
This topic is about the Json data type.
Value syntax
'{"name":"John", "age":30, "car":null, "cars":["Ford", "BMW", "Fiat"]}'
Description
Json data type contains a valid Json string and can be considered a special form of string whose content respects the Json standard specifications. If you take the string inside a Json data type and paste it into any JSON validator, the result should be a green light from the validator, meaning that the string is compliant with the JSON standard specifications.
The default value is an empty Json: ' {} '.
The Json type partially supports Comparison operators.
Json composition
XRS Script language supports JSON composition. Consider the following example.
Json j1='{"name":"John", "age":30, "car":null, "cars":["Ford", "BMW", "Fiat"]}'Json j2;
j2.AddObject("$","content");
j2.SetObject("$.content",j1);
It is possible to compose a new Json j2 starting from an existing one j1 or part of it. Because the Json data type is a reference type, any changes made to j1 will also affect j2.
It is also possible to copy a JSON by value using the ToString method.
j2 = j1.ToString();
Finally, cast from standard SJSON is also supported, thus it is possible to retrieve the JSON of an SJSON:
j2 = Runtime::Scene.GetFieldValue("testJson.value");
Implemented API
|
Method |
Return type |
Parameters |
Description |
|---|---|---|---|
|
AddArray |
|
string jPath, string keyName |
Returns a modified copy of the Json. Adds an element of type array as a child of the specified jPath element and with the key corresponding to the specified keyName. In cases where jPath references an element of type array, the keyName is ignored and can be omitted. |
|
AddObject |
|
string jPath, string keyName |
Returns a modified copy of the Json. Adds an element of type obj as a child of the specified jPath element and with the key corresponding to the specified keyName. In cases where jPath references an element of type array the keyName is ignored and can be omitted. |
|
AddValue |
|
string jPath, string keyName, string valueType |
Returns a modified copy of the Json. Adds an element of type valueType with the key corresponding to the specified keyName. It sets the value to the default of its value type. valueType is optional. If not specified, or if set to an unsupported value type, the string type is used as a default. Supported value types are string, int, double, bool. In cases where jPath references an element of type array, the keyName is ignored and can be omitted. |
|
Exists |
bool |
string jPath |
Returns true if the specified jPath corresponds to a key inside the Json value and false if otherwise. |
|
GetArray |
Json |
string jsonPath |
Returns a Json corresponding to the specified jsonPath element of type array. Keep in mind that this method uses the standard JSONPATH syntax. For further information on the JSONPATH syntax, refer to online documentation: https://stackoverflow.com/tags/jsonpath/info |
|
GetArrayLength |
int |
string jPath |
Returns the length of an array element value. If the specified jPath corresponds to an element of a different type than array, it returns -1. |
|
GetChildren |
List<string> |
string jsonPath |
Returns the list of children keys of the specified JSONPATH obj element. If the specified JSONPATH corresponds to a different type than obj, an empty list is returned. Keep in mind that this method uses the standard JSONPATH syntax. For further information on the JSONPATH syntax, please refer to online documentation: https://stackoverflow.com/tags/jsonpath/info |
|
GetObject |
Json |
string jsonPath |
Returns a Json corresponding to the specified jsonPath element of type obj. Keep in mind that this method uses the standard JSONPATH syntax. For further information on the JSONPATH syntax, please refer to online documentation: https://stackoverflow.com/tags/jsonpath/info |
|
GetType |
string |
string jPath |
Returns the type of the referenced key element. Types returned can be obj, array, or value. |
|
GetValueType |
string |
string JPath |
Returns the type of the referenced key element if it is a value, throw an exception otherwise. Types returned are always all lowercase. |
|
GetValue |
string |
string jPath |
Returns the value of the specified jPath value element. Even if values in a JSON document are of different types, this method will always return a string value. It is up to the user to eventually parse and cast the string to a different field data type in their code whenever it is required. |
|
JsonArrayToList |
List<string> |
string jsonPath |
Converts the json array to a list of string elements. Can be used only on Json arrays that contain only values, not other arrays or objects. |
|
SetArray |
|
string jPath, Json jValue |
Returns a modified copy of the Json. Sets an existing element of type array, child of the specified jPath element, to the specified jValue. Value can be either a Json or a List<T>. |
|
SetObject |
|
string jPath, Json jValue |
Returns a modified copy of the Json. Sets an existing element of type obj, child of the specified jPath element, to the specified jValue. |
|
SetValue |
|
string jPath, string value, string valueType |
Returns a modified copy of the Json. Sets the value of the specified jPath element of valueType to value. valueType is an optional parameter and it can be set as string, int, double, or bool. If it is not specified, the current value type of the element is preserved. |
|
ToString |
string |
|
Gets a string representation for the current value and allows to serialize json strings. |
|
Rem |
String jPath |
Modifies the Json removing the element corresponding to the specified jPath. |