JSON module
The JSON module contains functions for reading and writing JSON data.
JSON is a text-based data exchange format based on the syntax for constructing Javascript objects. In the context of a scripting language like FlowScript, JSON is a method for transforming data (like records, sequences, numbers, texts) into text format and back.
First, note that all JSON documents also valid FlowScript syntax. You can copy any piece JSON into a FlowScript program, and it is immediately a valid record, array or other value.
Here, for example, we are declaring a variable test and assigning it to a value copied from the first example from the W3Schools JSON introduction :
The variable test is now declared as a record with fields name, age and car.
While all JSON data is valid FlowScript syntax, the reverse does not hold. Some types of values in FlowScript (such as functions) cannot be transformed into JSON.
Serializing JSON
In order to turn a FlowScript value into JSON data â that is, a text variable containing the JSON representation of the value â we can use the serialize function in the JSON module.
Using the same example object as above, we can use the serialize function to transform it into text.
Trying to serialize a value of a function type will result in a runtime error:
Serializing binary data will convert the data to Base 64 text format.
Datetime values will be serialized as text in ISO 8601 format, including time zone offset:
Deserializing JSON
In order to turn JSON data (as a text value) into FlowScript objects, you use the deserialize function in the JSON module. In order to deserialize, we need to supply an expected type of the data. We supply that type as a type argument to the function.
Staying with the example mentioned above, here is how you might deserialize the data from a text variable.
If some member is declared in the type but missing in the JSON, it will be initialized with the member's default value (if set), or the default value for the member's type (if no default value set).
Sometimes it is necessary to distinguish members that are actually missing from the JSON. For such cases, we can deserialize as a smaller type containing only the members expected to be always present, and then use type narrowing to check for the presence of additional fields:
Narrow deserialization
If you wish to exclude data which is present in the JSON data but not a part of the type we deserialize to, you can use the deserializeNarrow function.
Last updated
Was this helpful?