FROM_JSON

The function FROM_JSON converts a JSON String to a value of an appropriate variable type.

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 TypeJSON typeExample
Nullnullnull
Booleanbooleantrue
Numbernumber3.141592653589793
Text (String)string"The quick brown fox"
Listarray[ 1, "two", 3 ]
App Objectobject{ "title": "Angels & Demons","author": "Dan Brown","isbn": "9781416524793","genres": ["Fiction"]}
DateTimeobject{ "date": { "year": 2007, "month": 1, "day": 9 },"time": { "hour": 9, "minute": 41, "second": 0, "millisecond": 0 }, "timeZone": "America/Los_Angeles"}
Dateobject{ "year": 2007, "month": 1, "day": 9}
Timeobject{ "hour": 9, "minute": 41, "second": 0, "millisecond": 0}
Currencyobject{ "amount": 100, "code": "USD", "precision": 2}

Declaration

FROM_JSON(json_string) -> airscript_value

Parameters

json_string (required, type: string)
A string containing valid JSON.

Return Values

airscript_value (type: any)
A value with the appropriate type given the contents of json_string.

Examples

FROM_JSON will automatically convert from a JSON type to an Airkit type. For example, a string containing a JSON number will be converted into a Number.

FROM_JSON("3.141592653589793") -> 3.141592653589793

A JSON string is enclosed in double quote characters and converts into a String.

FROM_JSON("\"The quick brown fox\"") -> "The quick brown fox"

Complex objects along with nested values will all be converted from JSON recursively.

FROM_JSON("[{"title":"Angels & Demons","author":"Dan Brown","isbn":"9781416524793","genres":["Fiction"]},{"title":"The Da Vinci Code","author":"Dan Brown","isbn":"9780307879257","genres":["Fiction"]},{"title":"Hackers: Heroes of the Computer Revolution","author":"Steven Levy","isbn":"9780141000510","genres":["History"]},{"title":"Moonwalking with Einstein: The Art and Science of Remembering Everything","author":"Joshua Foer","isbn":"9781594202292","genres":["Nonfiction"]}]")  
-> [
  { 
    title: "Angels & Demons",
    author: "Dan Brown",
    isbn: "9781416524793",
    genres: ["Fiction"]
  },
  {
    title: "The Da Vinci Code",
    author: "Dan Brown",
    isbn: "9780307879257",
    genres: ["Fiction"]
  },
  {
    title: "Hackers: Heroes of the Computer Revolution",
    author: "Steven Levy",
    isbn: "9780141000510",
    genres: ["History"]
  },
  {
    title: "Moonwalking with Einstein: The Art and Science of Remembering Everything",
    author: "Joshua Foer",
    isbn: "9781594202292",
    genres: ["Nonfiction"]
  }  
]

Discussion

The FROM_JSON function is the opposite of the TO_JSON function. The most common use for the FROM_JSON function is to parse data received from an external system. There are a few subtleties in accepting data that may contain values of the type DateTime, Date, Time, and Currency. It is very unlikely that the external system will represent these types the same way they are represented in Airkit, therefore, additional processing may be needed.

External systems will commonly represent DateTimes as either a number or as a String, commonly referred to as a timestamp. Airkt provides support for working with this common encoding technique, see Working with Date, Time & DateTime in Airscript for more details.