JSON features in MySQL 8.0
Listing some of the JSON functions in MySQL 8.0
1.JSON_PRETTY
The JSON data can now be fetched in a human readable format.
Advantages of JSON_PRETTY
Syntax:
SELECT JSON_PRETTY(json_column_name) FROM table_name ;Sample:
SELECT JSON_PRETTY('{"name": "John", "age": 30, "address": {"city": "Nagercoil", "state": "Tamilnadu"}}');Output:
{"name": "John","age": 30,"address": { "city": "Nagercoil", "state": "Tamilnadu"}}2.JSON_ARRAYAGG
A Set of JSON values will be aggregated and returned as a single JSON array.
Advantages of JSON_ARRAYAGG:
Syntax:
SELECT JSON_ARRAYAGG(json_column_name) FROM table_name;Sample:
SELECT JSON_ARRAYAGG(data) FROM (SELECT 1 AS data UNION ALL SELECT 2 UNION ALL SELECT 3) AS t;Output:
[1, 2, 3]3.JSON_MERGE
Used to merge two or more JSON documents into a single JSON document.
Advantages of JSON_MERGE:
Syntax:
SELECT JSON_MERGE(json_document[, json_document, ...]) FROM table_name;Sample:
SELECT JSON_MERGE('{"name": "John", "age": 30}','{"address": {"city": "Nagercoil", "state": "Tamilnadu"}}') AS final_doc;Output:
{"age": 30, "name": "John", "address": {"city": "Nagercoil", "state": "Tamilnadu"}}4.JSON_EXTRACT
Used to extract values from JSON documents stored in the database using a JSON path expression.
Advantages of JSON_EXTRACT:
Syntax:
SELECT JSON_EXTRACT(json_document, json_path) FROM table_name;Sample:
SELECT JSON_EXTRACT('{"name": "John", "age": 30, "address": {"city": "Nagercoil", "state": "Tamilnadu"}}', '$.name') AS name;Output:
John5.JSON_STORAGE_SIZE()
Shows the total number of bytes used to store the binary representation of a document.
Advantages of JSON_STORAGE_SIZE:
Syntax:
SELECT JSON_STORAGE_SIZE('JSON_DOCUMENT') FROM table_name;Sample:
SELECT data, JSON_STORAGE_SIZE(data) AS Size FROM bio;Output:
+----------------------------------------------------+------+| data | Size |+----------------------------------------------------+------+| {"age": 24, "name": "Abish", "Hobby": "[1, 2, 3]"} | 54 |+----------------------------------------------------+------+6.JSON_STORAGE_FREE()
Shows the amount of free space gained after changes made on the document.
Advantages of JSON_STORAGE_FREE:
Syntax:
SELECT JSON_STORAGE_FREE('JSON_DOCUMENT') FROM table_name;Sample:
Insert data:
INSERT INTO bio VALUES ('{"age": 24, "name": "Abish", "Hobby": "[1, 2, 3]"}') ;Update data:
UPDATE bio SET data = JSON_SET(data, "$.name", "AK");SELECT data,JSON_STORAGE_FREE(data) AS Free FROM bio;Output:
+-------------------------------------------------+------+| data | Free |+-------------------------------------------------+------+| {"age": 24, "name": "AK", "Hobby": "[1, 2, 3]"} | 9 |+-------------------------------------------------+------+7.JSON_OVERLAPS()
Compares two JSON documents and returns true (1) if the two documents have any same key-value or array elements.
Advantages of JSON_OVERLAPS:
Syntax:
SELECT JSON_OVERLAPS( [array / key_value1] , [array / key_value2] ) ;Sample 1:
SELECT JSON_OVERLAPS('{"a": 1, "b": 2}', '{"b": 3, "c": 4}');Output:
0Sample 2:
SELECT JSON_OVERLAPS('{"a": 1, "b": 2}', '{"a": 1, "b": 2}');Output:
18.JSON_SCHEMA_VALID()
Advantages of JSON_SCHEMA_VALID() :
Syntax:
SELECT JSON_SCHEMA_VALID (@schema, @document);Working sample:
Create Schema:
SET @schema = '{ "id": "101", "Schema_for_geo", "description": "A geographical coordinate", "type": "object", "properties": { "latitude": {"type": "number","minimum": -90,"maximum": 90},"longitude": {"type": "number","minimum": -180,"maximum": 180}}, "required": ["latitude", "longitude"] }';Create Document
SET @document = '{ "latitude": 63.444697, "longitude": 10.445118 }' ;Sample 1:
SELECT JSON_SCHEMA_VALID(@schema, @document);Output:
1Change Document:
SET @document = '{}';Sample 2:
SELECT JSON_SCHEMA_VALID(@schema, @document);Output:
09.JSON_SCHEMA_VALIDATION_REPORT()
Advantages of JSON_SCHEMA_VALIDATION_REPORT()
Syntax:
SELECT JSON_SCHEMA_VALIDATION_REPORT(schema,json_doc);Sample:
Output scenario 1:
SELECT JSON_SCHEMA_VALIDATION_REPORT(@schema,@cmp);+---------------------------------------------+| JSON_SCHEMA_VALIDATION_REPORT(@schema,@cmp) |+---------------------------------------------+| {"valid": true} |+---------------------------------------------+Output scenario 2:
SELECT JSON_PRETTY(JSON_SCHEMA_VALIDATION_REPORT(@schema,@cmp)) \GOutput:
*************************** 1. row ***************************JSON_PRETTY(JSON_SCHEMA_VALIDATION_REPORT(@schema,@cmp)): { "valid": false, "reason": "The JSON document location '#/salary' failed requirement 'maximum' at JSON Schema location '#/properties/salary'", "schema-location": "#/properties/salary", "document-location": "#/salary", "schema-failed-keyword": "maximum"}10.JSON_VALUE()
Advantages of JSON_VALUE()
Syntax:
SELECT JSON_VALUE(json_doc, path) ;Sample:
SELECT JSON_VALUE('{"name": "John", "age": 30, "isMarried": true}', '$.name');Output:
+------------------------------------------------------------------------+| JSON_VALUE('{"name": "John", "age": 30, "isMarried": true}', '$.name') |+------------------------------------------------------------------------+| John |+------------------------------------------------------------------------+11.MEMBER OF()
Advantages of MEMBER OF()
Syntax:
SELECT value MEMBER OF (<json_array>);Sample:
SELECT 'ab' MEMBER OF('[23, "abc", 17, "ab", 10]') as member;Output:
+------------+| member |+------------+| 1 |+------------+Miru IT Park, Vallankumaranvillai,
Nagercoil, Tamilnadu - 629 002.
Unit 303, Vanguard Rise,
5th Main, Konena Agrahara,
Old Airport Road, Bangalore - 560 017.
Call: +91 6383016411
Email: sales@mafiree.com