---
title: "JSON data type"
description: "The CoraDB JSON type: creating and validating JSON, JSON Schema constraints, conversion to and from SQL types, JSON paths and pointers."
url: "https://www.coradb.ai/manual/sql/datatype_json.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/datatype_json.md"
product: "CoraDB"
version: "1.0"
keywords: "coradb json type, json column, json schema validation, json path, json pointer, json_type, json conversion"
---

# JSON data type

## JSON data type

The **JSON** data type stores JSON documents as defined by [RFC 7159](https://tools.ietf.org/html/rfc7159). Values are validated when they are stored and kept in a parsed form, so the [JSON functions](https://www.coradb.ai/manual/sql/function/json_fn.html) can access their parts without parsing the text again.

### Creating JSON values

A string assigned to a **JSON** column is parsed automatically. A string can also be converted with [CAST](https://www.coradb.ai/manual/sql/function/typecast_fn.html#cast) or written as a `JSON'…'` literal, and [JSON_OBJECT](https://www.coradb.ai/manual/sql/function/json_fn.html#json_object) and [JSON_ARRAY](https://www.coradb.ai/manual/sql/function/json_fn.html#json_array) build JSON values from SQL values.

```sql
CREATE TABLE product_spec (product_id INT, spec JSON);
INSERT INTO product_spec VALUES (1618, '{"weight_g": 280, "colors": ["black", "white"]}');
SELECT product_id, spec, TYPEOF(spec) AS t, spec->>'$.colors[0]' AS first_color FROM product_spec;
```

| product_id | spec | t | first_color |
|---|---|---|---|
| 1618 | {"weight_g":280,"colors":["black","white"]} | json | black |

```sql
SELECT CAST('{"a":1}' AS JSON) AS a, JSON'{"a":1}' AS b, TYPEOF(JSON'{"a":1}') AS t;
```

| a | b | t |
|---|---|---|
| {"a":1} | {"a":1} | json |

JSON values are printed in compact form, without spaces between tokens.

### JSON validation

A string that is not valid JSON is rejected:

```sql
SELECT JSON'abc';
```

```text
ERROR: Invalid JSON: 'abc'.
```

A **JSON** column can also be restricted with a [JSON Schema](https://json-schema.org/specification.html) (see [Understanding JSON Schema](https://json-schema.org/understanding-json-schema/index.html) for an introduction). Write the schema as a string after the type:

```sql
CREATE TABLE product_spec (
    product_id INT,
    spec JSON ('{"type": "object",
                 "required": ["weight_g"],
                 "properties": {"weight_g": {"type": "integer"},
                                "colors":   {"type": "array", "items": {"type": "string"}}}}')
);

-- passes the schema
INSERT INTO product_spec VALUES (1618, '{"weight_g": 280, "colors": ["black", "white"]}');

-- "weight_g" is missing
INSERT INTO product_spec VALUES (1619, '{"colors": ["red"]}');
```

```text
ERROR: The provided JSON has been invalidated by the JSON schema (Invalid schema path: #, Keyword: required, Invalid provided JSON path: #)
```

[SHOW COLUMNS](https://www.coradb.ai/manual/sql/query/show.html#show-columns) shows the schema as part of the column type, for example `JSON('{"type": "object", …}')`. Casting a value to a **JSON** type with a schema also validates it.

### JSON value types

A JSON value is an object, an array, or a scalar (string, number, `true`, `false` or `null`), as defined by [RFC 7159](https://tools.ietf.org/html/rfc7159#section-3). [JSON_TYPE](https://www.coradb.ai/manual/sql/function/json_fn.html#json_type) returns the CoraDB name of the type:

| JSON value | CoraDB JSON type | Description |
|---|---|---|
| Object | `JSON_OBJECT` | A set of key-value pairs |
| Array | `JSON_ARRAY` | An ordered list of JSON values |
| string | `STRING` | A quoted string |
| Number | `INTEGER` | An integer that fits in 32 bits |
| Number | `BIGINT` | An integer that fits in 64 bits |
| Number | `DOUBLE` | Any other number: with a fraction, an exponent, or larger than 2^63 - 1 |
| `true`, `false` | `BOOLEAN` | A boolean |
| `null` | `JSON_NULL` | The JSON null value (not SQL **NULL**) |

```sql
SELECT JSON_TYPE(JSON'{"a":1}') AS a, JSON_TYPE(JSON'[1,2]') AS b, JSON_TYPE(JSON'"x"') AS c, JSON_TYPE(JSON'5') AS d,
       JSON_TYPE(JSON'5000000000') AS e, JSON_TYPE(JSON'5.5') AS f, JSON_TYPE(JSON'true') AS g, JSON_TYPE(JSON'null') AS h;
```

| a | b | c | d | e | f | g | h |
|---|---|---|---|---|---|---|---|
| JSON_OBJECT | JSON_ARRAY | STRING | INTEGER | BIGINT | DOUBLE | BOOLEAN | JSON_NULL |

### JSON data conversions

**To JSON.** Values of other types are converted to JSON explicitly with **CAST** or implicitly when stored in a **JSON** column or passed to a JSON function:

| SQL type | Result |
|---|---|
| Any string type | Parsed as JSON text; the result can be of any JSON type |
| **SHORT**, **INTEGER**, **BIGINT** | `INTEGER`, or `BIGINT` if the value does not fit in 32 bits |
| **FLOAT**, **DOUBLE**, **NUMERIC** | `DOUBLE` |

**From JSON.**

| CoraDB JSON type | Can be converted to |
|---|---|
| `JSON_OBJECT`, `JSON_ARRAY` | A string with the JSON text |
| `STRING` | Any type a string can be converted to; as a string, without the quotes |
| `INTEGER`, `BIGINT`, `DOUBLE` | Any type the corresponding SQL number can be converted to |
| `BOOLEAN` | `'true'` or `'false'` as a string; 1 or 0 as a number |
| `JSON_NULL` | The string `'null'` |

```sql
SELECT CAST(JSON'true' AS STRING) AS a, CAST(JSON'true' AS INT) AS b, CAST(JSON'"42"' AS INT) AS c,
       CAST(JSON'"abc"' AS STRING) AS d, CAST(JSON'null' AS STRING) AS e, JSON_TYPE(CAST(5.25 AS JSON)) AS f;
```

| a | b | c | d | e | f |
|---|---|---|---|---|---|
| true | 1 | 42 | abc | null | DOUBLE |

### JSON paths

Most JSON functions take a *JSON path* (or a JSON pointer, below) that addresses a part of a document. A path starts with `$`, the root, followed by any number of tokens:

```text
<json_path> ::= $ [ <path_token> ] ...

<path_token> ::= [ index ]                      -- array element, from 0
               | . key_identifier               -- object member
               | . "key string"                 -- object member, any key
               | .*                             -- every member of an object
               | [*]                            -- every element of an array
               | ** <path_token>                -- any sequence of keys and indexes, then the token
```

In `{"a":[0,1,2,{"b":5}]}`, the path `$.a[3].b` means "member `b` of element 3 of member `a` of the root" and addresses the value `5`. A key that is not a valid identifier is written in double quotes, with JSON escapes: `$."first name"`, or `$."\""` for a key that is a double quote.

Paths with wildcards can match several values; functions such as [JSON_EXTRACT](https://www.coradb.ai/manual/sql/function/json_fn.html#json_extract) then return them in an array:

```sql
SELECT JSON_EXTRACT('{"a":[0,1,2,{"b":5}]}', '$.a[3].b') AS a,
       JSON_EXTRACT('{"a":[0,1,2,{"b":5}]}', '$.a[*]') AS b,
       JSON_EXTRACT('{"a":{"x":1},"b":{"x":2}}', '$.*.x') AS c,
       JSON_EXTRACT('{"a":{"x":1},"b":[{"x":2}]}', '$**.x') AS d;
```

| a | b | c | d |
|---|---|---|---|
| 5 | [0,1,2,{"b":5}] | [1,2] | [1,2] |

Paths are interpreted in UTF-8. A path in another character set is converted to UTF-8 first.

### JSON pointers

A *JSON pointer* ([RFC 6901](https://tools.ietf.org/html/rfc6901)) is an alternative way to write a path without wildcards. Each token is preceded by `/`, and an empty pointer is the root:

```text
<json_pointer> ::= [ / path_token ] ... [ /- ]
```

| JSON path | JSON pointer |
|---|---|
| `$.a[10].bb` | `/a/10/bb` |
| `$` | (empty string) |

The special last token `/-` addresses the position after the last element of an array, for example to append a value: `JSON_INSERT('[1,2]', '/-', 3)` returns `[1,2,3]`.

```sql
SELECT JSON_EXTRACT('{"a":[0,1,2,{"b":5}]}', '/a/3/b') AS by_pointer, JSON_EXTRACT('{"a":1}', '') AS root;
```

| by_pointer | root |
|---|---|
| 5 | {"a":1} |

Like paths, pointers are interpreted in UTF-8.
