TO_JSON
Last updated
Was this helpful?
Last updated
Was this helpful?
The function TO_JSON converts any to a string by converting the data to .
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.
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}
value (required, type: any) Any Airscript value.
json_string (type: string) The input Airscript value serialized as a JSON string.
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.
TO_JSON called with a will return a String containing that number as if were called on that number.
When called with a , TO_JSON will return a string containing a JSON string, note that Text itself contains double quotes.
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 , , , and 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 for more details.