-
Posts
diff --git a/mysqljson/how-to-create-JSON-document-from-fields-mysql/index.html b/mysqljson/how-to-create-JSON-document-from-fields-mysql/index.html
index 11bbd4b1..8119dc1d 100644
--- a/mysqljson/how-to-create-JSON-document-from-fields-mysql/index.html
+++ b/mysqljson/how-to-create-JSON-document-from-fields-mysql/index.html
@@ -116,26 +116,26 @@
The dataset
The dataset is the following:
-{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}
-
+{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}
+
Check out the description of the fields
The following examples use a pizza order dataset with an order having:
@@ -147,48 +147,48 @@ The dataset
image
: null
- and two pizzas contained in the
pizzas
item:
-[
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
-]
-
+[
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+]
+
If you want to reproduce the examples, check how to recreate the dataset
It can be recreated with the following script:
-create table test(id serial primary key, json_data json);
-
-insert into test(json_data) values (
-'{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}');
-
+create table test(id serial primary key, json_data json);
+
+insert into test(json_data) values (
+'{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}');
+
Create a JSON object with JSON_OBJECT
function
To create a JSON document, from a list of fields or strings you can use the JSON_OBJECT
function.
-select
- JSON_OBJECT('mykey1','myvalue1','mykey2','myvalue2') json_data;
-
Result
+select
+ JSON_OBJECT('mykey1','myvalue1','mykey2','myvalue2') json_data;
+
Result
+----------------------------------------------+
| json_data |
+----------------------------------------------+
diff --git a/mysqljson/how-to-edit-JSON-document-mysql/index.html b/mysqljson/how-to-edit-JSON-document-mysql/index.html
index bd919784..82dc1748 100644
--- a/mysqljson/how-to-edit-JSON-document-mysql/index.html
+++ b/mysqljson/how-to-edit-JSON-document-mysql/index.html
@@ -120,26 +120,26 @@ The dataset
The dataset is the following:
-{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}
-
+{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}
+
Check out the description of the fields
The following examples use a pizza order dataset with an order having:
@@ -151,48 +151,48 @@ The dataset
image
: null
- and two pizzas contained in the
pizzas
item:
-[
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
-]
-
+[
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+]
+
If you want to reproduce the examples, check how to recreate the dataset
It can be recreated with the following script:
-create table test(id serial primary key, json_data json);
-
-insert into test(json_data) values (
-'{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}');
-
+create table test(id serial primary key, json_data json);
+
+insert into test(json_data) values (
+'{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}');
+
Edit a JSON document with JSON_SET
function
To edit a JSON document you can use the JSON_SET
function. To replace the second pizzaName
from Margherita
to Capricciosa
you can
-select
- JSON_SET(json_data,'$.pizzas[1].pizzaName','Capricciosa') from test;
-
Where
+select
+ JSON_SET(json_data,'$.pizzas[1].pizzaName','Capricciosa') from test;
+
Where
json_data
is the JSON column
$.pizzas[1].pizzaName
selects the pizzas
item, the 2nd element in the array and the pizzaName
subitem
@@ -204,9 +204,9 @@ Edit a JSON document with <
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Edit a JSON document with JSON_REPLACE
function
To edit a JSON document you can use the JSON_REPLACE
function. To replace the second pizzaName
from Margherita
to Capricciosa
you can
-select
- JSON_REPLACE(json_data,'$.pizzas[1].pizzaName','Capricciosa') from test;
-
Where
+select
+ JSON_REPLACE(json_data,'$.pizzas[1].pizzaName','Capricciosa') from test;
+
Where
json_data
is the JSON column
$.pizzas[1].pizzaName
selects the pizzas
item, the 2nd element in the array and the pizzaName
subitem
@@ -219,9 +219,9 @@ Edit a JSON document with <
| {"id": 778, "name": "Edward Olson", "shop": "Luigis Pizza", "image": null, "pizzas": [{"pizzaName": "Salami", "additionalToppings": ["🥓", "🌶️"]}, {"pizzaName": "Capricciosa", "additionalToppings": ["🍌", "🌶️", "🍍"]}], "address": "Unit 9398 Box 2056 DPO AP 24022", "phoneNumbers": ["(935)503-3765x4154", "(935)12345"]} |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Note: If you try to use the JSON_REPLACE
with a non existing field, like the $.nameS
, the function will NOT edit the document.
-
select
- JSON_REPLACE(json_data,'$.nameS','Ugo') from test;
-
The above doesn’t edit the document since the key nameS
is not present
+select
+ JSON_REPLACE(json_data,'$.nameS','Ugo') from test;
+
The above doesn’t edit the document since the key nameS
is not present
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| JSON_REPLACE(json_data,'$.nameS','Ugo') |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
diff --git a/mysqljson/how-to-extract-field-from-array-json-mysql/index.html b/mysqljson/how-to-extract-field-from-array-json-mysql/index.html
index 7bad980e..7bb4cdb9 100644
--- a/mysqljson/how-to-extract-field-from-array-json-mysql/index.html
+++ b/mysqljson/how-to-extract-field-from-array-json-mysql/index.html
@@ -116,26 +116,26 @@ The dataset
The dataset is the following:
-{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}
-
+{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}
+
Check out the description of the fields
The following examples use a pizza order dataset with an order having:
@@ -147,49 +147,49 @@ The dataset
image
: null
- and two pizzas contained in the
pizzas
item:
-[
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
-]
-
+[
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+]
+
If you want to reproduce the examples, check how to recreate the dataset
It can be recreated with the following script:
-create table test(id serial primary key, json_data json);
-
-insert into test(json_data) values (
-'{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}');
-
+create table test(id serial primary key, json_data json);
+
+insert into test(json_data) values (
+'{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}');
+
Using the ->
operator in conjunction with the [item_number]
JSON Path sintax, we can extract from an array. The [item_number]
syntax follows the JSON standards, therefore the array starting index is 0
. The second pizza in the order can be extracted with
-select
- json_data -> '$.pizzas[1]' second_pizza
- from test;
-
In the above query
+select
+ json_data -> '$.pizzas[1]' second_pizza
+ from test;
+
In the above query
json_data -> '$.pizzas'
extracts the pizzas
field
- the additional
[1]
extracts the second pizza (index starts from 0
)
diff --git a/mysqljson/how-to-extract-field-from-json-mysql/index.html b/mysqljson/how-to-extract-field-from-json-mysql/index.html
index 4efd4d53..59ca73c3 100644
--- a/mysqljson/how-to-extract-field-from-json-mysql/index.html
+++ b/mysqljson/how-to-extract-field-from-json-mysql/index.html
@@ -153,50 +153,50 @@ The dataset
image
: null
- and two pizzas contained in the
pizzas
item:
-[
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
-]
-
+[
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+]
+
If you want to reproduce the examples, check how to recreate the dataset
It can be recreated with the following script:
-create table test(id serial primary key, json_data json);
-
-insert into test(json_data) values (
-'{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}');
-
+create table test(id serial primary key, json_data json);
+
+insert into test(json_data) values (
+'{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}');
+
To extract a field value from a JSON document, you can use the ->
operator. The id
and name
fields can be extracted with:
-select
- json_data -> '$.id' id,
- json_data -> '$.name' name
- from test;
-
Result
+select
+ json_data -> '$.id' id,
+ json_data -> '$.name' name
+ from test;
+
Result
+------+----------------+
| id | name |
+------+----------------+
@@ -204,11 +204,11 @@
To extract a field from a JSON document as Text, you can use the ->
operator. The id
and name
fields can be extracted with:
-select
- json_data ->> '$.id' id,
- json_data ->> '$.name' order_name
-from test;
-
Result:
+select
+ json_data ->> '$.id' id,
+ json_data ->> '$.name' order_name
+from test;
+
Result:
+------+--------------+
| id | order_name |
+------+--------------+
@@ -216,11 +216,11 @@
To extract a field from a JSON document, you can use the JSON_EXTRACT
function. The id
and name
fields can be extracted with:
-select
- JSON_EXTRACT(json_data, '$.id') id,
- JSON_EXTRACT(json_data, '$.name') order_name
-from test;
-
Result:
+select
+ JSON_EXTRACT(json_data, '$.id') id,
+ JSON_EXTRACT(json_data, '$.name') order_name
+from test;
+
Result:
+------+----------------+
| id | order_name |
+------+----------------+
diff --git a/mysqljson/how-to-get-json-field-types-mysql/index.html b/mysqljson/how-to-get-json-field-types-mysql/index.html
index 9b1db532..e4740d35 100644
--- a/mysqljson/how-to-get-json-field-types-mysql/index.html
+++ b/mysqljson/how-to-get-json-field-types-mysql/index.html
@@ -116,26 +116,26 @@ The dataset
The dataset is the following:
-{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}
-
+{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}
+
Check out the description of the fields
The following examples use a pizza order dataset with an order having:
@@ -147,49 +147,49 @@ The dataset
image
: null
- and two pizzas contained in the
pizzas
item:
-[
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
-]
-
+[
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+]
+
If you want to reproduce the examples, check how to recreate the dataset
It can be recreated with the following script:
-create table test(id serial primary key, json_data json);
-
-insert into test(json_data) values (
-'{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}');
-
+create table test(id serial primary key, json_data json);
+
+insert into test(json_data) values (
+'{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}');
+
You can get the type of JSON data with the JSON_TYPE
function
-select
- JSON_TYPE(json_data -> '$.pizzas') pizzas
- from test;
-
In the above query
+select
+ JSON_TYPE(json_data -> '$.pizzas') pizzas
+ from test;
+
In the above query
{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}
-
+{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}
+
Check out the description of the fields
The following examples use a pizza order dataset with an order having:
@@ -147,48 +147,48 @@ The dataset
image
: null
- and two pizzas contained in the
pizzas
item:
-[
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
-]
-
+[
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+]
+
If you want to reproduce the examples, check how to recreate the dataset
It can be recreated with the following script:
-create table test(id serial primary key, json_data json);
-
-insert into test(json_data) values (
-'{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}');
-
+create table test(id serial primary key, json_data json);
+
+insert into test(json_data) values (
+'{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}');
+
Insert a field in a JSON document with JSON_INSERT
function
To insert a field in a JSON document you can use the JSON_INSERT
function. To insert the drinks
in the second pizza in the above order you can:
-select
- JSON_INSERT(json_data,'$.pizzas[1].drinks','Sparkling Water') extra_drink from test;
-
Where
+select
+ JSON_INSERT(json_data,'$.pizzas[1].drinks','Sparkling Water') extra_drink from test;
+
Where
json_data
is the JSON column
$.pizzas[1].drink
selects the pizzas
item, the 2nd element in the array and the drink
subitem
diff --git a/mysqljson/how-to-load-json-mysql/index.html b/mysqljson/how-to-load-json-mysql/index.html
index 84d9bb07..02d8e5ff 100644
--- a/mysqljson/how-to-load-json-mysql/index.html
+++ b/mysqljson/how-to-load-json-mysql/index.html
@@ -105,53 +105,53 @@
To load JSON data into a MySQL column you need to include it as string.
E.g. to include the following JSON:
-
{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}
-
In a table containing a json_data
JSON
column:
-create table test(id serial primary key, json_data json);
-
You can just insert the json_data
column as string:
-insert into test(id, json_data) values (
-1,
-'{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}');
-
MySQL will validate the content as JSON and, if correct, store the data.
+{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}
+
In a table containing a json_data
JSON
column:
+create table test(id serial primary key, json_data json);
+
You can just insert the json_data
column as string:
+insert into test(id, json_data) values (
+1,
+'{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}');
+
MySQL will validate the content as JSON and, if correct, store the data.
If you retrieve the data from the test
table with
-select * from test;
-
You can see the row correctly stored.
+You can see the row correctly stored.
+----+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id | json_data |
+----+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
diff --git a/mysqljson/how-to-merge-JSON-documents-mysql/index.html b/mysqljson/how-to-merge-JSON-documents-mysql/index.html
index 0756411a..183ba004 100644
--- a/mysqljson/how-to-merge-JSON-documents-mysql/index.html
+++ b/mysqljson/how-to-merge-JSON-documents-mysql/index.html
@@ -120,26 +120,26 @@ The dataset
The dataset is the following:
-{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}
-
+{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}
+
Check out the description of the fields
The following examples use a pizza order dataset with an order having:
@@ -151,43 +151,43 @@ The dataset
image
: null
- and two pizzas contained in the
pizzas
item:
-[
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
-]
-
+[
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+]
+
If you want to reproduce the examples, check how to recreate the dataset
It can be recreated with the following script:
-create table test(id serial primary key, json_data json);
-
-insert into test(json_data) values (
-'{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}');
-
+create table test(id serial primary key, json_data json);
+
+insert into test(json_data) values (
+'{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}');
+
Merge two JSON documents with JSON_MERGE_PRESERVE
function
To merge two (or more) JSON documents, by concatenating all the key values you can use the JSON_MERGE_PRESERVE
function.
select
@@ -203,12 +203,12 @@ Merge two JS
+------------------------------------------------------------------------------+
Merge two JSON documents with JSON_MERGE_PATCH
function
To merge two (or more) JSON documents, by keeping only the latest value for each key you can use the JSON_MERGE_PATCH
function.
-select
- JSON_MERGE_PATCH(
- '{"mykey1":"myvalue11","mykey2":"myvalue21"}',
- '{"mykey1":"myvalue12","mykey2":"myvalue22"}'
- ) json_data;
-
Result
+select
+ JSON_MERGE_PATCH(
+ '{"mykey1":"myvalue11","mykey2":"myvalue21"}',
+ '{"mykey1":"myvalue12","mykey2":"myvalue22"}'
+ ) json_data;
+
Result
+------------------------------------------------+
| json_data |
+------------------------------------------------+
diff --git a/mysqljson/how-to-remove-field-JSON-document-mysql/index.html b/mysqljson/how-to-remove-field-JSON-document-mysql/index.html
index 2fb0edd7..7dc67d4a 100644
--- a/mysqljson/how-to-remove-field-JSON-document-mysql/index.html
+++ b/mysqljson/how-to-remove-field-JSON-document-mysql/index.html
@@ -116,26 +116,26 @@ The dataset
The dataset is the following:
-{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}
-
+{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}
+
Check out the description of the fields
The following examples use a pizza order dataset with an order having:
@@ -147,48 +147,48 @@ The dataset
image
: null
- and two pizzas contained in the
pizzas
item:
-[
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
-]
-
+[
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+]
+
If you want to reproduce the examples, check how to recreate the dataset
It can be recreated with the following script:
-create table test(id serial primary key, json_data json);
-
-insert into test(json_data) values (
-'{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}');
-
+create table test(id serial primary key, json_data json);
+
+insert into test(json_data) values (
+'{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}');
+
Remove a field from a JSON document with JSON_REMOVE
function
To remove one or more fields from a JSON document you can use the JSON_REMOVE
function. To remove the the pizzaName
from the second pizza in the above order you can:
-select
- JSON_REMOVE(json_data,'$.pizzas[1].pizzaName') remove_pizzaName from test;
-
Where
+select
+ JSON_REMOVE(json_data,'$.pizzas[1].pizzaName') remove_pizzaName from test;
+
Where
{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}
-
+{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}
+
Check out the description of the fields
The following examples use a pizza order dataset with an order having:
@@ -147,56 +147,56 @@ The dataset
image
: null
- and two pizzas contained in the
pizzas
item:
-[
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
-]
-
+[
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+]
+
If you want to reproduce the examples, check how to recreate the dataset
It can be recreated with the following script:
-create table test(id serial primary key, json_data json);
-
-insert into test(json_data) values (
-'{
- "id": 778,
- "shop": "Luigis Pizza",
- "name": "Edward Olson",
- "phoneNumbers":
- ["(935)503-3765x4154","(935)12345"],
- "address": "Unit 9398 Box 2056 DPO AP 24022",
- "image": null,
- "pizzas": [
- {
- "pizzaName": "Salami",
- "additionalToppings": ["🥓", "🌶️"]
- },
- {
- "pizzaName": "Margherita",
- "additionalToppings": ["🍌", "🌶️", "🍍"]
- }
- ]
-}');
-
+create table test(id serial primary key, json_data json);
+
+insert into test(json_data) values (
+'{
+ "id": 778,
+ "shop": "Luigis Pizza",
+ "name": "Edward Olson",
+ "phoneNumbers":
+ ["(935)503-3765x4154","(935)12345"],
+ "address": "Unit 9398 Box 2056 DPO AP 24022",
+ "image": null,
+ "pizzas": [
+ {
+ "pizzaName": "Salami",
+ "additionalToppings": ["🥓", "🌶️"]
+ },
+ {
+ "pizzaName": "Margherita",
+ "additionalToppings": ["🍌", "🌶️", "🍍"]
+ }
+ ]
+}');
+
Tabulate a JSON document with JSON_TABLE
function
To retrieve one or more JSON documents as columns and rows you can use the JSON_TABLE
function. To retrieve the list of pizzas and their first additional topping from the above as table you can:
-select tbl.* from
- test,
- JSON_TABLE(
- json_data,
- '$.pizzas[*]'
- COLUMNS (
- pizzaName VARCHAR(100) PATH '$.pizzaName',
- additionalToppings VARCHAR(100) PATH '$.additionalToppings[0]'
- )
- ) tbl;
-
Where
+select tbl.* from
+ test,
+ JSON_TABLE(
+ json_data,
+ '$.pizzas[*]'
+ COLUMNS (
+ pizzaName VARCHAR(100) PATH '$.pizzaName',
+ additionalToppings VARCHAR(100) PATH '$.additionalToppings[0]'
+ )
+ ) tbl;
+
Where
json_data
is the JSON column
'$.pizzas[*]'
generates a row for each pizza in the pizzas
array
diff --git a/mysqljson/index.html b/mysqljson/index.html
index 10553cbd..cff4f043 100644
--- a/mysqljson/index.html
+++ b/mysqljson/index.html
@@ -94,6 +94,36 @@
+
+
+
+
+
+
+
+
+
+
+
You can query a JSON document in MySQL to find content within it with:
+
+- The
JSON_CONTAINS
function that will return if a JSON document is contained within another JSON document. The function returns 1
if the document is contained, 0
elsewhere.
+- The
JSON_CONTAINS
function that will return if a JSON document path is contained within another JSON document. The function returns 1
if the document is contained, 0
elsewhere.
+
+
Continue Reading
+
+
+
+
+
+
+