TO_JSON
Last updated
Last updated
The function TO_JSON converts any any variable type to a string by converting the data to JSON.
All Airkit types can be converted to and from JSON in such a way that serializing to JSON, and deserializing that JSON will return the original value. The following table lists how each data type is encoded as JSON.
Airkit Variable Type | JSON type | Example |
---|---|---|
value (required, type: any) Any Airscript value.
json_string (type: string) The input Airscript value serialized as a JSON string.
TO_JSON called with a Number will return a String containing that number as if FORMAT_NUMBER were called on that number.
When called with a String, TO_JSON will return a string containing a JSON string, note that Text itself contains double quotes.
Complex objects along with nested values will all be converted to JSON recursively.
The last example contained extra white space to aid legibility, however, the real output will not actually contain whitespace and the actual result will look like this.
The most common use case for serializing values to JSON is to send data to an external system. Special care needs to be taken when serializing values of the type DateTime, Date, Time, and Currency as there are no standard ways to represent these types in JSON. It is unlikely that the external system will represent them in the same way as Airkit does.
While Date, Time, and Currency types have no industry standard representation, DateTime types are commonly represented as either a number or a string in JSON. Airkit provides functions for working with these common formats, called timestamps. See Working with Date, Time & DateTime in Airscript for more details.
null
null
boolean
true
number
3.141592653589793
string
"The quick brown fox"
array
[ 1, "two", 3 ]
object
{ "title": "Angels & Demons","author": "Dan Brown","isbn": "9781416524793", "genres": ["Fiction"]}
object
{ "date": { "year": 2007, "month": 1, "day": 9 }, "time": { "hour": 9, "minute": 41, "second": 0, "millisecond": 0 }, "timeZone": "America/Los_Angeles"}
object
{ "year": 2007, "month": 1, "day": 9}
object
{ "hour": 9, "minute": 41, "second": 0, "millisecond": 0}
object
{ "amount": 100, "code": "USD", "precision": 2}