JSON

SinceVersion 1.2.0

NOTICE: In version 1.2.x the data type name is JSONB. It’s renamed to JSON to be more compatible to version 2.0.0. And the old tables can still be used.

description

  1. JSON (Binary) datatype.
  2. Use binary JSON format for storage and json function to extract field. Default support is 1048576 bytes (1M), adjustable up to 2147483643 bytes (2G),and the JSONB type is also limited by the be configuration `jsonb_type_length_soft_limit_bytes`.

note

  1. There are some advantanges for JSON over plain JSON STRING.
  2. 1. JSON syntax will be validated on write to ensure data quality
  3. 2. JSON binary format is more efficient. Using json_extract functions on JSON datatype is 2-4 times faster than get_json_xx on JSON STRING format.

example

A tutorial for JSON datatype including create table, load data and query.

create database and table

  1. CREATE DATABASE testdb;
  2. USE testdb;
  3. CREATE TABLE test_json (
  4. id INT,
  5. j JSON
  6. )
  7. DUPLICATE KEY(id)
  8. DISTRIBUTED BY HASH(id) BUCKETS 10
  9. PROPERTIES("replication_num" = "1");

Load data

stream load test_json.csv test data
  • there are 2 columns, the 1st column is id and the 2nd column is json string
  • there are 25 rows, the first 18 rows are valid json and the last 7 rows are invalid
  1. 1 \N
  2. 2 null
  3. 3 true
  4. 4 false
  5. 5 100
  6. 6 10000
  7. 7 1000000000
  8. 8 1152921504606846976
  9. 9 6.18
  10. 10 "abcd"
  11. 11 {}
  12. 12 {"k1":"v31", "k2": 300}
  13. 13 []
  14. 14 [123, 456]
  15. 15 ["abc", "def"]
  16. 16 [null, true, false, 100, 6.18, "abc"]
  17. 17 [{"k1":"v41", "k2": 400}, 1, "a", 3.14]
  18. 18 {"k1":"v31", "k2": 300, "a1": [{"k1":"v41", "k2": 400}, 1, "a", 3.14]}
  19. 19 ''
  20. 20 'abc'
  21. 21 abc
  22. 22 100x
  23. 23 6.a8
  24. 24 {x
  25. 25 [123, abc]
  • due to the 28% of rows is invalid,stream load with default configuration will fail with error message “too many filtered rows”
  1. curl --location-trusted -u root: -T test_json.csv http://127.0.0.1:8840/api/testdb/test_json/_stream_load
  2. {
  3. "TxnId": 12019,
  4. "Label": "744d9821-9c9f-43dc-bf3b-7ab048f14e32",
  5. "TwoPhaseCommit": "false",
  6. "Status": "Fail",
  7. "Message": "too many filtered rows",
  8. "NumberTotalRows": 25,
  9. "NumberLoadedRows": 18,
  10. "NumberFilteredRows": 7,
  11. "NumberUnselectedRows": 0,
  12. "LoadBytes": 380,
  13. "LoadTimeMs": 48,
  14. "BeginTxnTimeMs": 0,
  15. "StreamLoadPutTimeMs": 1,
  16. "ReadDataTimeMs": 0,
  17. "WriteDataTimeMs": 45,
  18. "CommitAndPublishTimeMs": 0,
  19. "ErrorURL": "http://172.21.0.5:8840/api/_load_error_log?file=__shard_2/error_log_insert_stmt_95435c4bf5f156df-426735082a9296af_95435c4bf5f156df_426735082a9296af"
  20. }
  • stream load will success after set header configuration ‘max_filter_ratio: 0.3’
  1. curl --location-trusted -u root: -H 'max_filter_ratio: 0.3' -T test_json.csv http://127.0.0.1:8840/api/testdb/test_json/_stream_load
  2. {
  3. "TxnId": 12017,
  4. "Label": "f37a50c1-43e9-4f4e-a159-a3db6abe2579",
  5. "TwoPhaseCommit": "false",
  6. "Status": "Success",
  7. "Message": "OK",
  8. "NumberTotalRows": 25,
  9. "NumberLoadedRows": 18,
  10. "NumberFilteredRows": 7,
  11. "NumberUnselectedRows": 0,
  12. "LoadBytes": 380,
  13. "LoadTimeMs": 68,
  14. "BeginTxnTimeMs": 0,
  15. "StreamLoadPutTimeMs": 2,
  16. "ReadDataTimeMs": 0,
  17. "WriteDataTimeMs": 45,
  18. "CommitAndPublishTimeMs": 19,
  19. "ErrorURL": "http://172.21.0.5:8840/api/_load_error_log?file=__shard_0/error_log_insert_stmt_a1463f98a7b15caf-c79399b920f5bfa3_a1463f98a7b15caf_c79399b920f5bfa3"
  20. }
  • use SELECT to view the data loaded by stream load. The column with JSON type will be displayed as plain JSON string.
  1. mysql> SELECT * FROM test_json ORDER BY id;
  2. +------+---------------------------------------------------------------+
  3. | id | j |
  4. +------+---------------------------------------------------------------+
  5. | 1 | NULL |
  6. | 2 | null |
  7. | 3 | true |
  8. | 4 | false |
  9. | 5 | 100 |
  10. | 6 | 10000 |
  11. | 7 | 1000000000 |
  12. | 8 | 1152921504606846976 |
  13. | 9 | 6.18 |
  14. | 10 | "abcd" |
  15. | 11 | {} |
  16. | 12 | {"k1":"v31","k2":300} |
  17. | 13 | [] |
  18. | 14 | [123,456] |
  19. | 15 | ["abc","def"] |
  20. | 16 | [null,true,false,100,6.18,"abc"] |
  21. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] |
  22. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} |
  23. +------+---------------------------------------------------------------+
  24. 18 rows in set (0.03 sec)
write data using insert into
  • total rows increae from 18 to 19 after insert 1 row
  1. mysql> INSERT INTO test_json VALUES(26, '{"k1":"v1", "k2": 200}');
  2. Query OK, 1 row affected (0.09 sec)
  3. {'label':'insert_4ece6769d1b42fd_ac9f25b3b8f3dc02', 'status':'VISIBLE', 'txnId':'12016'}
  4. mysql> SELECT * FROM test_json ORDER BY id;
  5. +------+---------------------------------------------------------------+
  6. | id | j |
  7. +------+---------------------------------------------------------------+
  8. | 1 | NULL |
  9. | 2 | null |
  10. | 3 | true |
  11. | 4 | false |
  12. | 5 | 100 |
  13. | 6 | 10000 |
  14. | 7 | 1000000000 |
  15. | 8 | 1152921504606846976 |
  16. | 9 | 6.18 |
  17. | 10 | "abcd" |
  18. | 11 | {} |
  19. | 12 | {"k1":"v31","k2":300} |
  20. | 13 | [] |
  21. | 14 | [123,456] |
  22. | 15 | ["abc","def"] |
  23. | 16 | [null,true,false,100,6.18,"abc"] |
  24. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] |
  25. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} |
  26. | 26 | {"k1":"v1","k2":200} |
  27. +------+---------------------------------------------------------------+
  28. 19 rows in set (0.03 sec)

Query

extract some filed from json by json_extract functions
  1. extract the whole json, ‘$’ stands for root in json path
  1. +------+---------------------------------------------------------------+---------------------------------------------------------------+
  2. | id | j | json_extract(`j`, '$') |
  3. +------+---------------------------------------------------------------+---------------------------------------------------------------+
  4. | 1 | NULL | NULL |
  5. | 2 | null | null |
  6. | 3 | true | true |
  7. | 4 | false | false |
  8. | 5 | 100 | 100 |
  9. | 6 | 10000 | 10000 |
  10. | 7 | 1000000000 | 1000000000 |
  11. | 8 | 1152921504606846976 | 1152921504606846976 |
  12. | 9 | 6.18 | 6.18 |
  13. | 10 | "abcd" | "abcd" |
  14. | 11 | {} | {} |
  15. | 12 | {"k1":"v31","k2":300} | {"k1":"v31","k2":300} |
  16. | 13 | [] | [] |
  17. | 14 | [123,456] | [123,456] |
  18. | 15 | ["abc","def"] | ["abc","def"] |
  19. | 16 | [null,true,false,100,6.18,"abc"] | [null,true,false,100,6.18,"abc"] |
  20. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | [{"k1":"v41","k2":400},1,"a",3.14] |
  21. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} |
  22. | 26 | {"k1":"v1","k2":200} | {"k1":"v1","k2":200} |
  23. +------+---------------------------------------------------------------+---------------------------------------------------------------+
  24. 19 rows in set (0.03 sec)
  1. extract k1 field, return NULL if it does not exist
  1. mysql> SELECT id, j, json_extract(j, '$.k1') FROM test_json ORDER BY id;
  2. +------+---------------------------------------------------------------+----------------------------+
  3. | id | j | json_extract(`j`, '$.k1') |
  4. +------+---------------------------------------------------------------+----------------------------+
  5. | 1 | NULL | NULL |
  6. | 2 | null | NULL |
  7. | 3 | true | NULL |
  8. | 4 | false | NULL |
  9. | 5 | 100 | NULL |
  10. | 6 | 10000 | NULL |
  11. | 7 | 1000000000 | NULL |
  12. | 8 | 1152921504606846976 | NULL |
  13. | 9 | 6.18 | NULL |
  14. | 10 | "abcd" | NULL |
  15. | 11 | {} | NULL |
  16. | 12 | {"k1":"v31","k2":300} | "v31" |
  17. | 13 | [] | NULL |
  18. | 14 | [123,456] | NULL |
  19. | 15 | ["abc","def"] | NULL |
  20. | 16 | [null,true,false,100,6.18,"abc"] | NULL |
  21. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | NULL |
  22. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | "v31" |
  23. | 26 | {"k1":"v1","k2":200} | "v1" |
  24. +------+---------------------------------------------------------------+----------------------------+
  25. 19 rows in set (0.03 sec)
  1. extract element 0 of the top level array
  1. mysql> SELECT id, j, json_extract(j, '$[0]') FROM test_json ORDER BY id;
  2. +------+---------------------------------------------------------------+----------------------------+
  3. | id | j | json_extract(`j`, '$[0]') |
  4. +------+---------------------------------------------------------------+----------------------------+
  5. | 1 | NULL | NULL |
  6. | 2 | null | NULL |
  7. | 3 | true | NULL |
  8. | 4 | false | NULL |
  9. | 5 | 100 | NULL |
  10. | 6 | 10000 | NULL |
  11. | 7 | 1000000000 | NULL |
  12. | 8 | 1152921504606846976 | NULL |
  13. | 9 | 6.18 | NULL |
  14. | 10 | "abcd" | NULL |
  15. | 11 | {} | NULL |
  16. | 12 | {"k1":"v31","k2":300} | NULL |
  17. | 13 | [] | NULL |
  18. | 14 | [123,456] | 123 |
  19. | 15 | ["abc","def"] | "abc" |
  20. | 16 | [null,true,false,100,6.18,"abc"] | null |
  21. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | {"k1":"v41","k2":400} |
  22. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | NULL |
  23. | 26 | {"k1":"v1","k2":200} | NULL |
  24. +------+---------------------------------------------------------------+----------------------------+
  25. 19 rows in set (0.03 sec)
  1. extract a whole json array of name a1
  1. mysql> SELECT id, j, json_extract(j, '$.a1') FROM test_json ORDER BY id;
  2. +------+---------------------------------------------------------------+------------------------------------+
  3. | id | j | json_extract(`j`, '$.a1') |
  4. +------+---------------------------------------------------------------+------------------------------------+
  5. | 1 | NULL | NULL |
  6. | 2 | null | NULL |
  7. | 3 | true | NULL |
  8. | 4 | false | NULL |
  9. | 5 | 100 | NULL |
  10. | 6 | 10000 | NULL |
  11. | 7 | 1000000000 | NULL |
  12. | 8 | 1152921504606846976 | NULL |
  13. | 9 | 6.18 | NULL |
  14. | 10 | "abcd" | NULL |
  15. | 11 | {} | NULL |
  16. | 12 | {"k1":"v31","k2":300} | NULL |
  17. | 13 | [] | NULL |
  18. | 14 | [123,456] | NULL |
  19. | 15 | ["abc","def"] | NULL |
  20. | 16 | [null,true,false,100,6.18,"abc"] | NULL |
  21. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | NULL |
  22. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | [{"k1":"v41","k2":400},1,"a",3.14] |
  23. | 26 | {"k1":"v1","k2":200} | NULL |
  24. +------+---------------------------------------------------------------+------------------------------------+
  25. 19 rows in set (0.02 sec)
  1. extract nested field from an object in an array
  1. mysql> SELECT id, j, json_extract(j, '$.a1[0]'), json_extract(j, '$.a1[0].k1') FROM test_json ORDER BY id;
  2. +------+---------------------------------------------------------------+-------------------------------+----------------------------------+
  3. | id | j | json_extract(`j`, '$.a1[0]') | json_extract(`j`, '$.a1[0].k1') |
  4. +------+---------------------------------------------------------------+-------------------------------+----------------------------------+
  5. | 1 | NULL | NULL | NULL |
  6. | 2 | null | NULL | NULL |
  7. | 3 | true | NULL | NULL |
  8. | 4 | false | NULL | NULL |
  9. | 5 | 100 | NULL | NULL |
  10. | 6 | 10000 | NULL | NULL |
  11. | 7 | 1000000000 | NULL | NULL |
  12. | 8 | 1152921504606846976 | NULL | NULL |
  13. | 9 | 6.18 | NULL | NULL |
  14. | 10 | "abcd" | NULL | NULL |
  15. | 11 | {} | NULL | NULL |
  16. | 12 | {"k1":"v31","k2":300} | NULL | NULL |
  17. | 13 | [] | NULL | NULL |
  18. | 14 | [123,456] | NULL | NULL |
  19. | 15 | ["abc","def"] | NULL | NULL |
  20. | 16 | [null,true,false,100,6.18,"abc"] | NULL | NULL |
  21. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | NULL | NULL |
  22. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | {"k1":"v41","k2":400} | "v41" |
  23. | 26 | {"k1":"v1","k2":200} | NULL | NULL |
  24. +------+---------------------------------------------------------------+-------------------------------+----------------------------------+
  25. 19 rows in set (0.02 sec)
  1. extract field with specific datatype
  • json_extract_string will extract field with string type,convert to string if the field is not string
  1. mysql> SELECT id, j, json_extract_string(j, '$') FROM test_json ORDER BY id;
  2. +------+---------------------------------------------------------------+---------------------------------------------------------------+
  3. | id | j | json_extract_string(`j`, '$') |
  4. +------+---------------------------------------------------------------+---------------------------------------------------------------+
  5. | 1 | NULL | NULL |
  6. | 2 | null | null |
  7. | 3 | true | true |
  8. | 4 | false | false |
  9. | 5 | 100 | 100 |
  10. | 6 | 10000 | 10000 |
  11. | 7 | 1000000000 | 1000000000 |
  12. | 8 | 1152921504606846976 | 1152921504606846976 |
  13. | 9 | 6.18 | 6.18 |
  14. | 10 | "abcd" | abcd |
  15. | 11 | {} | {} |
  16. | 12 | {"k1":"v31","k2":300} | {"k1":"v31","k2":300} |
  17. | 13 | [] | [] |
  18. | 14 | [123,456] | [123,456] |
  19. | 15 | ["abc","def"] | ["abc","def"] |
  20. | 16 | [null,true,false,100,6.18,"abc"] | [null,true,false,100,6.18,"abc"] |
  21. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | [{"k1":"v41","k2":400},1,"a",3.14] |
  22. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} |
  23. | 26 | {"k1":"v1","k2":200} | {"k1":"v1","k2":200} |
  24. +------+---------------------------------------------------------------+---------------------------------------------------------------+
  25. 19 rows in set (0.02 sec)
  26. mysql> SELECT id, j, json_extract_string(j, '$.k1') FROM test_json ORDER BY id;
  27. +------+---------------------------------------------------------------+-----------------------------------+
  28. | id | j | json_extract_string(`j`, '$.k1') |
  29. +------+---------------------------------------------------------------+-----------------------------------+
  30. | 1 | NULL | NULL |
  31. | 2 | null | NULL |
  32. | 3 | true | NULL |
  33. | 4 | false | NULL |
  34. | 5 | 100 | NULL |
  35. | 6 | 10000 | NULL |
  36. | 7 | 1000000000 | NULL |
  37. | 8 | 1152921504606846976 | NULL |
  38. | 9 | 6.18 | NULL |
  39. | 10 | "abcd" | NULL |
  40. | 11 | {} | NULL |
  41. | 12 | {"k1":"v31","k2":300} | v31 |
  42. | 13 | [] | NULL |
  43. | 14 | [123,456] | NULL |
  44. | 15 | ["abc","def"] | NULL |
  45. | 16 | [null,true,false,100,6.18,"abc"] | NULL |
  46. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | NULL |
  47. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | v31 |
  48. | 26 | {"k1":"v1","k2":200} | v1 |
  49. +------+---------------------------------------------------------------+-----------------------------------+
  50. 19 rows in set (0.03 sec)
  • json_extract_int will extract field with int type,return NULL if the field is not int
  1. mysql> SELECT id, j, json_extract_int(j, '$') FROM test_json ORDER BY id;
  2. +------+---------------------------------------------------------------+-----------------------------+
  3. | id | j | json_extract_int(`j`, '$') |
  4. +------+---------------------------------------------------------------+-----------------------------+
  5. | 1 | NULL | NULL |
  6. | 2 | null | NULL |
  7. | 3 | true | NULL |
  8. | 4 | false | NULL |
  9. | 5 | 100 | 100 |
  10. | 6 | 10000 | 10000 |
  11. | 7 | 1000000000 | 1000000000 |
  12. | 8 | 1152921504606846976 | NULL |
  13. | 9 | 6.18 | NULL |
  14. | 10 | "abcd" | NULL |
  15. | 11 | {} | NULL |
  16. | 12 | {"k1":"v31","k2":300} | NULL |
  17. | 13 | [] | NULL |
  18. | 14 | [123,456] | NULL |
  19. | 15 | ["abc","def"] | NULL |
  20. | 16 | [null,true,false,100,6.18,"abc"] | NULL |
  21. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | NULL |
  22. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | NULL |
  23. | 26 | {"k1":"v1","k2":200} | NULL |
  24. +------+---------------------------------------------------------------+-----------------------------+
  25. 19 rows in set (0.02 sec)
  26. mysql> SELECT id, j, json_extract_int(j, '$.k2') FROM test_json ORDER BY id;
  27. +------+---------------------------------------------------------------+--------------------------------+
  28. | id | j | json_extract_int(`j`, '$.k2') |
  29. +------+---------------------------------------------------------------+--------------------------------+
  30. | 1 | NULL | NULL |
  31. | 2 | null | NULL |
  32. | 3 | true | NULL |
  33. | 4 | false | NULL |
  34. | 5 | 100 | NULL |
  35. | 6 | 10000 | NULL |
  36. | 7 | 1000000000 | NULL |
  37. | 8 | 1152921504606846976 | NULL |
  38. | 9 | 6.18 | NULL |
  39. | 10 | "abcd" | NULL |
  40. | 11 | {} | NULL |
  41. | 12 | {"k1":"v31","k2":300} | 300 |
  42. | 13 | [] | NULL |
  43. | 14 | [123,456] | NULL |
  44. | 15 | ["abc","def"] | NULL |
  45. | 16 | [null,true,false,100,6.18,"abc"] | NULL |
  46. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | NULL |
  47. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | 300 |
  48. | 26 | {"k1":"v1","k2":200} | 200 |
  49. +------+---------------------------------------------------------------+--------------------------------+
  50. 19 rows in set (0.03 sec)
  • json_extract_bigint will extract field with bigint type,return NULL if the field is not bigint
  1. mysql> SELECT id, j, json_extract_bigint(j, '$') FROM test_json ORDER BY id;
  2. +------+---------------------------------------------------------------+--------------------------------+
  3. | id | j | json_extract_bigint(`j`, '$') |
  4. +------+---------------------------------------------------------------+--------------------------------+
  5. | 1 | NULL | NULL |
  6. | 2 | null | NULL |
  7. | 3 | true | NULL |
  8. | 4 | false | NULL |
  9. | 5 | 100 | 100 |
  10. | 6 | 10000 | 10000 |
  11. | 7 | 1000000000 | 1000000000 |
  12. | 8 | 1152921504606846976 | 1152921504606846976 |
  13. | 9 | 6.18 | NULL |
  14. | 10 | "abcd" | NULL |
  15. | 11 | {} | NULL |
  16. | 12 | {"k1":"v31","k2":300} | NULL |
  17. | 13 | [] | NULL |
  18. | 14 | [123,456] | NULL |
  19. | 15 | ["abc","def"] | NULL |
  20. | 16 | [null,true,false,100,6.18,"abc"] | NULL |
  21. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | NULL |
  22. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | NULL |
  23. | 26 | {"k1":"v1","k2":200} | NULL |
  24. +------+---------------------------------------------------------------+--------------------------------+
  25. 19 rows in set (0.03 sec)
  26. mysql> SELECT id, j, json_extract_bigint(j, '$.k2') FROM test_json ORDER BY id;
  27. +------+---------------------------------------------------------------+-----------------------------------+
  28. | id | j | json_extract_bigint(`j`, '$.k2') |
  29. +------+---------------------------------------------------------------+-----------------------------------+
  30. | 1 | NULL | NULL |
  31. | 2 | null | NULL |
  32. | 3 | true | NULL |
  33. | 4 | false | NULL |
  34. | 5 | 100 | NULL |
  35. | 6 | 10000 | NULL |
  36. | 7 | 1000000000 | NULL |
  37. | 8 | 1152921504606846976 | NULL |
  38. | 9 | 6.18 | NULL |
  39. | 10 | "abcd" | NULL |
  40. | 11 | {} | NULL |
  41. | 12 | {"k1":"v31","k2":300} | 300 |
  42. | 13 | [] | NULL |
  43. | 14 | [123,456] | NULL |
  44. | 15 | ["abc","def"] | NULL |
  45. | 16 | [null,true,false,100,6.18,"abc"] | NULL |
  46. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | NULL |
  47. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | 300 |
  48. | 26 | {"k1":"v1","k2":200} | 200 |
  49. +------+---------------------------------------------------------------+-----------------------------------+
  50. 19 rows in set (0.02 sec)
  • json_extract_double will extract field with double type,return NULL if the field is not double
  1. mysql> SELECT id, j, json_extract_double(j, '$') FROM test_json ORDER BY id;
  2. +------+---------------------------------------------------------------+--------------------------------+
  3. | id | j | json_extract_double(`j`, '$') |
  4. +------+---------------------------------------------------------------+--------------------------------+
  5. | 1 | NULL | NULL |
  6. | 2 | null | NULL |
  7. | 3 | true | NULL |
  8. | 4 | false | NULL |
  9. | 5 | 100 | 100 |
  10. | 6 | 10000 | 10000 |
  11. | 7 | 1000000000 | 1000000000 |
  12. | 8 | 1152921504606846976 | 1.152921504606847e+18 |
  13. | 9 | 6.18 | 6.18 |
  14. | 10 | "abcd" | NULL |
  15. | 11 | {} | NULL |
  16. | 12 | {"k1":"v31","k2":300} | NULL |
  17. | 13 | [] | NULL |
  18. | 14 | [123,456] | NULL |
  19. | 15 | ["abc","def"] | NULL |
  20. | 16 | [null,true,false,100,6.18,"abc"] | NULL |
  21. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | NULL |
  22. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | NULL |
  23. | 26 | {"k1":"v1","k2":200} | NULL |
  24. +------+---------------------------------------------------------------+--------------------------------+
  25. 19 rows in set (0.02 sec)
  26. mysql> SELECT id, j, json_extract_double(j, '$.k2') FROM test_json ORDER BY id;
  27. +------+---------------------------------------------------------------+-----------------------------------+
  28. | id | j | json_extract_double(`j`, '$.k2') |
  29. +------+---------------------------------------------------------------+-----------------------------------+
  30. | 1 | NULL | NULL |
  31. | 2 | null | NULL |
  32. | 3 | true | NULL |
  33. | 4 | false | NULL |
  34. | 5 | 100 | NULL |
  35. | 6 | 10000 | NULL |
  36. | 7 | 1000000000 | NULL |
  37. | 8 | 1152921504606846976 | NULL |
  38. | 9 | 6.18 | NULL |
  39. | 10 | "abcd" | NULL |
  40. | 11 | {} | NULL |
  41. | 12 | {"k1":"v31","k2":300} | 300 |
  42. | 13 | [] | NULL |
  43. | 14 | [123,456] | NULL |
  44. | 15 | ["abc","def"] | NULL |
  45. | 16 | [null,true,false,100,6.18,"abc"] | NULL |
  46. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | NULL |
  47. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | 300 |
  48. | 26 | {"k1":"v1","k2":200} | 200 |
  49. +------+---------------------------------------------------------------+-----------------------------------+
  50. 19 rows in set (0.03 sec)
  • json_extract_bool will extract field with boolean type,return NULL if the field is not boolean
  1. mysql> SELECT id, j, json_extract_bool(j, '$') FROM test_json ORDER BY id;
  2. +------+---------------------------------------------------------------+------------------------------+
  3. | id | j | json_extract_bool(`j`, '$') |
  4. +------+---------------------------------------------------------------+------------------------------+
  5. | 1 | NULL | NULL |
  6. | 2 | null | NULL |
  7. | 3 | true | 1 |
  8. | 4 | false | 0 |
  9. | 5 | 100 | NULL |
  10. | 6 | 10000 | NULL |
  11. | 7 | 1000000000 | NULL |
  12. | 8 | 1152921504606846976 | NULL |
  13. | 9 | 6.18 | NULL |
  14. | 10 | "abcd" | NULL |
  15. | 11 | {} | NULL |
  16. | 12 | {"k1":"v31","k2":300} | NULL |
  17. | 13 | [] | NULL |
  18. | 14 | [123,456] | NULL |
  19. | 15 | ["abc","def"] | NULL |
  20. | 16 | [null,true,false,100,6.18,"abc"] | NULL |
  21. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | NULL |
  22. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | NULL |
  23. | 26 | {"k1":"v1","k2":200} | NULL |
  24. +------+---------------------------------------------------------------+------------------------------+
  25. 19 rows in set (0.01 sec)
  26. mysql> SELECT id, j, json_extract_bool(j, '$[1]') FROM test_json ORDER BY id;
  27. +------+---------------------------------------------------------------+---------------------------------+
  28. | id | j | json_extract_bool(`j`, '$[1]') |
  29. +------+---------------------------------------------------------------+---------------------------------+
  30. | 1 | NULL | NULL |
  31. | 2 | null | NULL |
  32. | 3 | true | NULL |
  33. | 4 | false | NULL |
  34. | 5 | 100 | NULL |
  35. | 6 | 10000 | NULL |
  36. | 7 | 1000000000 | NULL |
  37. | 8 | 1152921504606846976 | NULL |
  38. | 9 | 6.18 | NULL |
  39. | 10 | "abcd" | NULL |
  40. | 11 | {} | NULL |
  41. | 12 | {"k1":"v31","k2":300} | NULL |
  42. | 13 | [] | NULL |
  43. | 14 | [123,456] | NULL |
  44. | 15 | ["abc","def"] | NULL |
  45. | 16 | [null,true,false,100,6.18,"abc"] | 1 |
  46. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | NULL |
  47. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | NULL |
  48. | 26 | {"k1":"v1","k2":200} | NULL |
  49. +------+---------------------------------------------------------------+---------------------------------+
  50. 19 rows in set (0.01 sec)
  • json_extract_isnull will extract field with json null type,return 1 if the field is json null , else 0
  • json null is different from SQL NULL. SQL NULL stands for no value for a field, but json null stands for an field with special value null.
  1. mysql> SELECT id, j, json_extract_isnull(j, '$') FROM test_json ORDER BY id;
  2. +------+---------------------------------------------------------------+--------------------------------+
  3. | id | j | json_extract_isnull(`j`, '$') |
  4. +------+---------------------------------------------------------------+--------------------------------+
  5. | 1 | NULL | NULL |
  6. | 2 | null | 1 |
  7. | 3 | true | 0 |
  8. | 4 | false | 0 |
  9. | 5 | 100 | 0 |
  10. | 6 | 10000 | 0 |
  11. | 7 | 1000000000 | 0 |
  12. | 8 | 1152921504606846976 | 0 |
  13. | 9 | 6.18 | 0 |
  14. | 10 | "abcd" | 0 |
  15. | 11 | {} | 0 |
  16. | 12 | {"k1":"v31","k2":300} | 0 |
  17. | 13 | [] | 0 |
  18. | 14 | [123,456] | 0 |
  19. | 15 | ["abc","def"] | 0 |
  20. | 16 | [null,true,false,100,6.18,"abc"] | 0 |
  21. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | 0 |
  22. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | 0 |
  23. | 26 | {"k1":"v1","k2":200} | 0 |
  24. +------+---------------------------------------------------------------+--------------------------------+
  25. 19 rows in set (0.03 sec)
check if a field is existed in json by json_exists_path
  1. mysql> SELECT id, j, json_exists_path(j, '$') FROM test_json ORDER BY id;
  2. +------+---------------------------------------------------------------+-----------------------------+
  3. | id | j | json_exists_path(`j`, '$') |
  4. +------+---------------------------------------------------------------+-----------------------------+
  5. | 1 | NULL | NULL |
  6. | 2 | null | 1 |
  7. | 3 | true | 1 |
  8. | 4 | false | 1 |
  9. | 5 | 100 | 1 |
  10. | 6 | 10000 | 1 |
  11. | 7 | 1000000000 | 1 |
  12. | 8 | 1152921504606846976 | 1 |
  13. | 9 | 6.18 | 1 |
  14. | 10 | "abcd" | 1 |
  15. | 11 | {} | 1 |
  16. | 12 | {"k1":"v31","k2":300} | 1 |
  17. | 13 | [] | 1 |
  18. | 14 | [123,456] | 1 |
  19. | 15 | ["abc","def"] | 1 |
  20. | 16 | [null,true,false,100,6.18,"abc"] | 1 |
  21. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | 1 |
  22. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | 1 |
  23. | 26 | {"k1":"v1","k2":200} | 1 |
  24. +------+---------------------------------------------------------------+-----------------------------+
  25. 19 rows in set (0.02 sec)
  26. mysql> SELECT id, j, json_exists_path(j, '$.k1') FROM test_json ORDER BY id;
  27. +------+---------------------------------------------------------------+--------------------------------+
  28. | id | j | json_exists_path(`j`, '$.k1') |
  29. +------+---------------------------------------------------------------+--------------------------------+
  30. | 1 | NULL | NULL |
  31. | 2 | null | 0 |
  32. | 3 | true | 0 |
  33. | 4 | false | 0 |
  34. | 5 | 100 | 0 |
  35. | 6 | 10000 | 0 |
  36. | 7 | 1000000000 | 0 |
  37. | 8 | 1152921504606846976 | 0 |
  38. | 9 | 6.18 | 0 |
  39. | 10 | "abcd" | 0 |
  40. | 11 | {} | 0 |
  41. | 12 | {"k1":"v31","k2":300} | 1 |
  42. | 13 | [] | 0 |
  43. | 14 | [123,456] | 0 |
  44. | 15 | ["abc","def"] | 0 |
  45. | 16 | [null,true,false,100,6.18,"abc"] | 0 |
  46. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | 0 |
  47. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | 1 |
  48. | 26 | {"k1":"v1","k2":200} | 1 |
  49. +------+---------------------------------------------------------------+--------------------------------+
  50. 19 rows in set (0.03 sec)
  51. mysql> SELECT id, j, json_exists_path(j, '$[2]') FROM test_json ORDER BY id;
  52. +------+---------------------------------------------------------------+--------------------------------+
  53. | id | j | json_exists_path(`j`, '$[2]') |
  54. +------+---------------------------------------------------------------+--------------------------------+
  55. | 1 | NULL | NULL |
  56. | 2 | null | 0 |
  57. | 3 | true | 0 |
  58. | 4 | false | 0 |
  59. | 5 | 100 | 0 |
  60. | 6 | 10000 | 0 |
  61. | 7 | 1000000000 | 0 |
  62. | 8 | 1152921504606846976 | 0 |
  63. | 9 | 6.18 | 0 |
  64. | 10 | "abcd" | 0 |
  65. | 11 | {} | 0 |
  66. | 12 | {"k1":"v31","k2":300} | 0 |
  67. | 13 | [] | 0 |
  68. | 14 | [123,456] | 0 |
  69. | 15 | ["abc","def"] | 0 |
  70. | 16 | [null,true,false,100,6.18,"abc"] | 1 |
  71. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | 1 |
  72. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | 0 |
  73. | 26 | {"k1":"v1","k2":200} | 0 |
  74. +------+---------------------------------------------------------------+--------------------------------+
  75. 19 rows in set (0.02 sec)
get the datatype of a field in json by json_type
  • return the data type of the field specified by json path, NULL if not existed.
  1. mysql> SELECT id, j, json_type(j, '$') FROM test_json ORDER BY id;
  2. +------+---------------------------------------------------------------+----------------------+
  3. | id | j | json_type(`j`, '$') |
  4. +------+---------------------------------------------------------------+----------------------+
  5. | 1 | NULL | NULL |
  6. | 2 | null | null |
  7. | 3 | true | bool |
  8. | 4 | false | bool |
  9. | 5 | 100 | int |
  10. | 6 | 10000 | int |
  11. | 7 | 1000000000 | int |
  12. | 8 | 1152921504606846976 | bigint |
  13. | 9 | 6.18 | double |
  14. | 10 | "abcd" | string |
  15. | 11 | {} | object |
  16. | 12 | {"k1":"v31","k2":300} | object |
  17. | 13 | [] | array |
  18. | 14 | [123,456] | array |
  19. | 15 | ["abc","def"] | array |
  20. | 16 | [null,true,false,100,6.18,"abc"] | array |
  21. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | array |
  22. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | object |
  23. | 26 | {"k1":"v1","k2":200} | object |
  24. +------+---------------------------------------------------------------+----------------------+
  25. 19 rows in set (0.02 sec)
  26. mysql> select id, j, json_type(j, '$.k1') from test_json order by id;
  27. +------+---------------------------------------------------------------+-------------------------+
  28. | id | j | json_type(`j`, '$.k1') |
  29. +------+---------------------------------------------------------------+-------------------------+
  30. | 1 | NULL | NULL |
  31. | 2 | null | NULL |
  32. | 3 | true | NULL |
  33. | 4 | false | NULL |
  34. | 5 | 100 | NULL |
  35. | 6 | 10000 | NULL |
  36. | 7 | 1000000000 | NULL |
  37. | 8 | 1152921504606846976 | NULL |
  38. | 9 | 6.18 | NULL |
  39. | 10 | "abcd" | NULL |
  40. | 11 | {} | NULL |
  41. | 12 | {"k1":"v31","k2":300} | string |
  42. | 13 | [] | NULL |
  43. | 14 | [123,456] | NULL |
  44. | 15 | ["abc","def"] | NULL |
  45. | 16 | [null,true,false,100,6.18,"abc"] | NULL |
  46. | 17 | [{"k1":"v41","k2":400},1,"a",3.14] | NULL |
  47. | 18 | {"k1":"v31","k2":300,"a1":[{"k1":"v41","k2":400},1,"a",3.14]} | string |
  48. | 26 | {"k1":"v1","k2":200} | string |
  49. +------+---------------------------------------------------------------+-------------------------+
  50. 19 rows in set (0.03 sec)

keywords

JSONB, JSON, json_parse, json_parse_error_to_null, json_parse_error_to_value, json_extract, json_extract_isnull, json_extract_bool, json_extract_int, json_extract_bigint, json_extract_double, json_extract_string, json_exists_path, json_type