Query a multi-level JSON object stored in MySQL

Asked
Active3 hr before
Viewed126 times

5 Answers

multilevelqueryobjectmysql
90%

Defining a JSON column in a SingleStore DB table is as simple as specifying the JSON data type in the CREATE TABLE command:,This section describes how to insert and update data in a SingleStore DB table with one or more JSON columns,,It is recommended that users define JSON columns with the NOT NULL suffix

Example_snippet/controller/utility/_multi.js/ '{ "b\u000a": 1,"a": 2 ,"a":3 . . .
'{ "b\u000a": 1,"a": 2 ,"a":3 } '
Step 2 continued with '{"a":3,"b\n":1}' . . .
'{"a":3,"b\n":1}'
load more v
88%

MySQL supports a native JSON data type defined by RFC 7159 that enables efficient access to data in JSON (JavaScript Object Notation) documents, The JSON data type provides these advantages over storing JSON-format strings in a string column: , The following table provides a summary of the rules that MySQL follows when casting between JSON values and values of other types: ,

Example_snippet/controller/utility/_multi.js/ ["abc", 10, null, true, false]. . .
["abc", 10, null, true, false]
Step 2 continued with {"k1": "value", "k2": 10}. . .
{
   "k1": "value",
   "k2": 10
}
Step 3 continued with ["12:18:29.000000", "2015-07-2. . .
["12:18:29.000000", "2015-07-29", "2015-07-29 12:18:29.000000"]
Step 4 continued with [99, {"id": "HK500", "cost": 7. . .
[99, {
      "id": "HK500",
      "cost": 75.99
   },
   ["hot", "cold"]
] {
   "k1": "value",
   "k2": [10, 20]
}
Step 5 continued with mysql> CREATE TABLE t1 (jdoc J. . .
mysql > CREATE TABLE t1(jdoc JSON);
Query OK, 0 rows affected(0.20 sec)

mysql > INSERT INTO t1 VALUES('{"key1": "value1", "key2": "value2"}');
Query OK, 1 row affected(0.01 sec)

mysql > INSERT INTO t1 VALUES('[1, 2,');
ERROR 3140(22032) at line 2: Invalid JSON text:
   "Invalid value."
at position 6 in value(or column)
'[1, 2,'.
Step 6 continued with mysql> SELECT JSON_TYPE('["a",. . .
mysql > SELECT JSON_TYPE('["a", "b", 1]'); +
-- -- -- -- -- -- -- -- -- -- -- -- -- -- +
|
JSON_TYPE('["a", "b", 1]') |
   + -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
   |
   ARRAY |
   + -- -- -- -- -- -- -- -- -- -- -- -- -- -- +

   mysql > SELECT JSON_TYPE('"hello"'); +
-- -- -- -- -- -- -- -- -- -- -- +
|
JSON_TYPE('"hello"') |
   + -- -- -- -- -- -- -- -- -- -- -- +
   |
   STRING |
   + -- -- -- -- -- -- -- -- -- -- -- +

   mysql > SELECT JSON_TYPE('hello');
ERROR 3146(22032): Invalid data type
for JSON data in argument 1
to
function json_type;
a JSON string or JSON type is required.
Step 7 continued with mysql> SELECT JSON_ARRAY('a', . . .
mysql > SELECT JSON_ARRAY('a', 1, NOW()); +
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
|
JSON_ARRAY('a', 1, NOW()) |
   + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
   |
   ["a", 1, "2015-07-27 09:43:47.000000"] |
   + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +
Step 8 continued with mysql> SELECT JSON_OBJECT('key. . .
mysql > SELECT JSON_OBJECT('key1', 1, 'key2', 'abc'); +
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
|
JSON_OBJECT('key1', 1, 'key2', 'abc') |
   + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
   |
   {
      "key1": 1,
      "key2": "abc"
   } |
   + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
Step 9 continued with mysql> SELECT JSON_MERGE_PRESE. . .
mysql > SELECT JSON_MERGE_PRESERVE('["a", 1]', '{"key": "value"}'); +
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
|
JSON_MERGE_PRESERVE('["a", 1]', '{"key": "value"}') |
   + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
   |
   ["a", 1, {
      "key": "value"
   }] |
   + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
   1 row in set(0.00 sec)
Step 10 continued with mysql> SET @j = JSON_OBJECT('k. . .
mysql > SET @j = JSON_OBJECT('key', 'value');
mysql > SELECT @j; +
-- -- -- -- -- -- -- -- -- +
|
@j |
   + -- -- -- -- -- -- -- -- -- +
   |
   {
      "key": "value"
   } |
   + -- -- -- -- -- -- -- -- -- +
Step 11 continued with mysql> SELECT CHARSET(@j), COL. . .
mysql > SELECT CHARSET(@j), COLLATION(@j); +
-- -- -- -- -- -- - + -- -- -- -- -- -- -- - +
|
CHARSET(@j) | COLLATION(@j) |
   + -- -- -- -- -- -- - + -- -- -- -- -- -- -- - +
   |
   utf8mb4 | utf8mb4_bin |
   + -- -- -- -- -- -- - + -- -- -- -- -- -- -- - +
Step 12 continued with mysql> SELECT JSON_ARRAY('x') . . .
mysql > SELECT JSON_ARRAY('x') = JSON_ARRAY('X'); +
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
|
JSON_ARRAY('x') = JSON_ARRAY('X') |
   + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
   |
   0 |
   + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
Step 13 continued with mysql> SELECT JSON_VALID('null. . .
mysql > SELECT JSON_VALID('null'), JSON_VALID('Null'), JSON_VALID('NULL'); +
-- -- -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- -- -- +
|
JSON_VALID('null') | JSON_VALID('Null') | JSON_VALID('NULL') |
   + -- -- -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- -- -- +
   |
   1 | 0 | 0 |
   + -- -- -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- -- -- +

   mysql > SELECT CAST('null'
      AS JSON); +
-- -- -- -- -- -- -- -- -- -- -- +
|
CAST('null'
      AS JSON) |
   + -- -- -- -- -- -- -- -- -- -- -- +
   |
   null |
   + -- -- -- -- -- -- -- -- -- -- -- +
   1 row in set(0.00 sec)

mysql > SELECT CAST('NULL'
   AS JSON);
ERROR 3141(22032): Invalid JSON text in argument 1 to
function cast_as_json:
"Invalid value."
at position 0 in 'NULL'.
Step 14 continued with mysql> SELECT ISNULL(null), IS. . .
mysql > SELECT ISNULL(null), ISNULL(Null), ISNULL(NULL); +
-- -- -- -- -- -- -- + -- -- -- -- -- -- -- + -- -- -- -- -- -- -- +
|
ISNULL(null) | ISNULL(Null) | ISNULL(NULL) |
   + -- -- -- -- -- -- -- + -- -- -- -- -- -- -- + -- -- -- -- -- -- -- +
   |
   1 | 1 | 1 |
   + -- -- -- -- -- -- -- + -- -- -- -- -- -- -- + -- -- -- -- -- -- -- +
Step 15 continued with mysql> CREATE TABLE facts (sen. . .
mysql > CREATE TABLE facts(sentence JSON);
Step 16 continued with mascot: The MySQL mascot is a . . .
mascot: The MySQL mascot is a dolphin named "Sakila".
Step 17 continued with mysql> INSERT INTO facts VALUE. . .
mysql > INSERT INTO facts VALUES >
   (JSON_OBJECT("mascot", "Our mascot is a dolphin named \"Sakila\"."));
Step 18 continued with mysql> INSERT INTO facts VALUE. . .
mysql > INSERT INTO facts VALUES >
   ('{"mascot": "Our mascot is a dolphin named \\"Sakila\\"."}');
Step 19 continued with mysql> SELECT sentence FROM fa. . .
mysql > SELECT sentence FROM facts; +
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
|
sentence |
   + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
   |
   {
      "mascot": "Our mascot is a dolphin named \"Sakila\"."
   } |
   + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
Step 20 continued with mysql> SELECT col->"$.mascot" . . .
mysql > SELECT col - > "$.mascot"
FROM qtest; +
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
|
col - > "$.mascot" |
   + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
   |
   "Our mascot is a dolphin named \"Sakila\"." |
   + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
   1 row in set(0.00 sec)
Step 21 continued with mysql> SELECT sentence->>"$.ma. . .
mysql > SELECT sentence - >> "$.mascot"
FROM facts; +
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
|
sentence - >> "$.mascot" |
   + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
   |
   Our mascot is a dolphin named "Sakila". |
   + -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - +
Step 22 continued with mysql> INSERT INTO facts VALUE. . .
mysql > INSERT INTO facts VALUES >
   (JSON_OBJECT('mascot', 'Our mascot is a dolphin named "Sakila".'));
load more v
72%

Normalization is a technique used to optimize the database structure, The First Normal Form (1NF) rule governs that every column should hold a single value — which is clearly broken by storing multi-value JSON documents

Example_snippet/controller/utility/_multi.js/ CREATE TABLE `book` ( `id` M. . .
CREATE TABLE `book`(
   `id`
   MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
   `title`
   VARCHAR(200) NOT NULL,
   `tags`
   JSON DEFAULT NULL,
   PRIMARY KEY(`id`)
) ENGINE = INNODB;
Step 2 continued with INSERT INTO `book` (`title`, `. . .
INSERT INTO `book`(`title`, `tags`)
VALUES(
   'ECMAScript 2015: A SitePoint Anthology',
   '["JavaScript", "ES2015", "JSON"]'
);
Step 3 continued with -- returns [1, 2, "abc"]: SELE. . .
--returns[1, 2, "abc"]:
   SELECT JSON_ARRAY(1, 2, 'abc');
Step 4 continued with -- returns {"a": 1, "b": 2}: S. . .
--returns {
   "a": 1,
   "b": 2
}:
SELECT JSON_OBJECT('a', 1, 'b', 2);
Step 5 continued with -- returns "[1, 2, \"abc\"]": . . .
--returns "[1, 2, \"abc\"]":
   SELECT JSON_QUOTE('[1, 2, "abc"]');
Step 6 continued with -- returns ARRAY: SELECT JSON_. . .
--returns ARRAY:
   SELECT JSON_TYPE('[1, 2, "abc"]');

--returns OBJECT:
   SELECT JSON_TYPE('{"a": 1, "b": 2}');

--returns an error:
   SELECT JSON_TYPE('{"a": 1, "b": 2');
Step 7 continued with -- returns 1: SELECT JSON_TYPE. . .
--returns 1:
   SELECT JSON_TYPE('[1, 2, "abc"]');

--returns 1:
   SELECT JSON_TYPE('{"a": 1, "b": 2}');

--returns 0:
   SELECT JSON_TYPE('{"a": 1, "b": 2');
Step 8 continued with -- all books with the 'JavaScr. . .
--all books with the 'JavaScript'
tag:
   SELECT * FROM `book`
WHERE JSON_CONTAINS(tags, '["JavaScript"]');
Step 9 continued with -- all books with tags startin. . .
--all books with tags starting 'Java':
   SELECT * FROM `book`
WHERE JSON_SEARCH(tags, 'one', 'Java%') IS NOT NULL;
Step 10 continued with -- returns "SitePoint": SELECT. . .
--returns "SitePoint":
   SELECT JSON_EXTRACT('{"id": 1, "website": "SitePoint"}', '$.website');
Step 11 continued with { "a": 1, "b": 2, "c": [. . .
{
   "a": 1,
   "b": 2,
   "c": [3, 4],
   "d": {
      "e": 5,
      "f": 6
   }
}
Step 12 continued with SELECT title, tags->"$[0]" A. . .
SELECT
title, tags - > "$[0]"
AS `tag1`
FROM `book`;
Step 13 continued with SELECT name, profile->"$.twi. . .
SELECT
name, profile - > "$.twitter"
AS `twitter`
FROM `user`;
Step 14 continued with SELECT name, profile->"$.twi. . .
SELECT
name, profile - > "$.twitter"
AS `twitter`
FROM `user`
WHERE
profile - > "$.twitter"
IS NOT NULL;
Step 15 continued with UPDATE `book` SET tags = JSO. . .
UPDATE `book`
SET tags = JSON_MERGE(tags, '["technical"]')
WHERE
JSON_SEARCH(tags, 'one', 'JavaScript') IS NOT NULL;
load more v
65%

Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,I have a JSON column in a MySQL table that contains a multi-level JSON object, I can access the values at the first level using the function JSON_EXTRACT but I can't find how to go over the first level

Example_snippet/controller/utility/_multi.js/ '$.data_sources.provider3' SE. . .
'$.data_sources.provider3'

SELECT symbol, JSON_EXTRACT(metadata, '$.data_sources.provider3) 
      FROM ref_data_table WHERE symbol = 'VXX';
load more v
75%

Relational databases are beginning to support document types like JSON, It’s an easy, flexible data type to create but can be painful to query

Example_snippet/controller/utility/_level.js/ select my_json_field ->> '. . .
select
my_json_field - >> 'userId',
   my_json_field - > 'transaction' - >> 'id',
   my_json_field - > 'transaction' - >> 'sku'
from my_table;
load more v