# TO\_JSON

**The function TO\_JSON converts any** [**any variable type**](https://docs.airkit.ai/?q=working-with-date,-time-and-datetime-in-airscript) **to a string by converting the data to** [**JSON**](https://en.wikipedia.org/wiki/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                                                                                                                                                    |
| ---------------------------------------------------------------------- | --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Null](../../variable-data-types/the-null-data-type)                   | null      | `null`                                                                                                                                                     |
| [Boolean](../../variable-data-types/the-boolean-variable-data-type)    | boolean   | `true`                                                                                                                                                     |
| [Number](../../variable-data-types/the-number-variable-data-type)      | number    | `3.141592653589793`                                                                                                                                        |
| [Text (String)](../../variable-data-types/the-text-variable-data-type) | string    | `"The quick brown fox"`                                                                                                                                    |
| [List](../../variable-data-types/the-list-data-type)                   | array     | `[ 1, "two", 3 ]`                                                                                                                                          |
| [App Object](../../working-with-data/airdata/airdata-app-objects)      | object    | `{ "title": "Angels & Demons","author": "Dan Brown","isbn": "9781416524793", "genres": ["Fiction"]}`                                                       |
| [DateTime](../../variable-data-types/the-datetime-variable-data-type)  | object    | `{ "date": { "year": 2007, "month": 1, "day": 9 }, "time": { "hour": 9, "minute": 41, "second": 0, "millisecond": 0 }, "timeZone": "America/Los_Angeles"}` |
| [Date](../../variable-data-types/the-date-variable-data-type)          | object    | `{ "year": 2007, "month": 1, "day": 9}`                                                                                                                    |
| [Time](../../variable-data-types/the-time-variable-data-type)          | object    | `{ "hour": 9, "minute": 41, "second": 0, "millisecond": 0}`                                                                                                |
| [Currency](../../variable-data-types/the-currency-variable-data-type)  | object    | `{ "amount": 100, "code": "USD", "precision": 2}`                                                                                                          |

#### Declaration

```javascript
TO_JSON(value) -> json_string
```

#### Parameters

**value** (required, type: _any_) Any Airscript value.

#### Return Values

**json\_string** (type: _string_) The input Airscript value serialized as a JSON string.

#### Examples

TO\_JSON called with a [Number](../../variable-data-types/the-number-variable-data-type) will return a String containing that number as if [FORMAT\_NUMBER](../number-formatting-functions/format_number) were called on that number.

```javascript
TO_JSON(PI()) -> "3.141592653589793"
```

When called with a [String](../../variable-data-types/the-text-variable-data-type), TO\_JSON will return a string containing a JSON string, note that Text itself contains double quotes.

```javascript
TO_JSON("The quick brown fox") -> "\"The quick brown fox\""
```

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

```javascript
TO_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"
      ]
   }
]
```

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.

```javascript
TO_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 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](../../variable-data-types/the-datetime-variable-data-type), [Date](../../variable-data-types/the-date-variable-data-type), [Time](../../variable-data-types/the-time-variable-data-type), and [Currency](../../variable-data-types/the-currency-variable-data-type) 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](https://docs.airkit.ai/?q=working-with-date,-time-and-datetime-in-airscript) for more details.
