10-json.md 2.5 KB
Newer Older
1 2 3 4 5 6
---
title: JSON Type
---

## Syntax

7
1. Tag of type JSON
8 9 10 11 12 13 14

   ```sql
   create STable s1 (ts timestamp, v1 int) tags (info json);

   create table s1_1 using s1 tags ('{"k1": "v1"}');
   ```

15
2. "->" Operator of JSON
16 17 18 19 20 21 22

   ```sql
   select * from s1 where info->'k1' = 'v1';

   select info->'k1' from s1;
   ```

23
3. "contains" Operator of JSON
24 25 26 27 28 29 30 31 32

   ```sql
   select * from s1 where info contains 'k2';

   select * from s1 where info contains 'k1';
   ```

## Applicable Operations

33
1. When a JSON data type is used in `where`, `match/nmatch/between and/like/and/or/is null/is no null` can be used but `in` can't be used.
34 35 36 37 38 39 40 41 42 43 44

   ```sql
   select * from s1 where info->'k1' match 'v*';

   select * from s1 where info->'k1' like 'v%' and info contains 'k2';

   select * from s1 where info is null;

   select * from s1 where info->'k1' is not null;
   ```

45
2. A tag of JSON type can be used in `group by`, `order by`, `join`, `union all` and sub query; for example `group by json->'key'`
46

47
3. `Distinct` can be used with a tag of type JSON
48 49 50 51 52 53 54

   ```sql
   select distinct info->'k1' from s1;
   ```

4. Tag Operations

55
   The value of a JSON tag can be altered. Please note that the full JSON will be overriden when doing this.
56

57
   The name of a JSON tag can be altered. A tag of JSON type can't be added or removed. The column length of a JSON tag can't be changed.
58 59 60

## Other Restrictions

S
Sean Ely 已提交
61
- JSON type can only be used for a tag. There can be only one tag of JSON type, and it's exclusive to any other types of tags.
62 63 64 65 66

- The maximum length of keys in JSON is 256 bytes, and key must be printable ASCII characters. The maximum total length of a JSON is 4,096 bytes.

- JSON format:

67 68 69
  - The input string for JSON can be empty, i.e. "", "\t", or NULL, but it can't be non-NULL string, bool or array.
  - object can be {}, and the entire JSON is empty if so. Key can be "", and it's ignored if so.
  - value can be int, double, string, bool or NULL, and it can't be an array. Nesting is not allowed which means that the value of a key can't be JSON.
70 71 72
  - If one key occurs twice in JSON, only the first one is valid.
  - Escape characters are not allowed in JSON.

73
- NULL is returned when querying a key that doesn't exist in JSON.
74 75 76

- If a tag of JSON is the result of inner query, it can't be parsed and queried in the outer query.

77
For example, the SQL statements below are not supported.
78 79 80 81 82

```sql;
select jtag->'key' from (select jtag from STable);
select jtag->'key' from (select jtag from STable) where jtag->'key'>0;
```