SlideShare a Scribd company logo
JSON
Validation
Dave Stokes
MySQL Community Manager
MySQL Community Team
Dave Stokes
MySQL Community Team
Oracle Corporation
@Stoker
https://elephantdolphin.blogspot.com/
David.Stokes@Oracle.com
Slides are available at Slideshare.net/davestokes
2
3
JSON
JSON (JavaScript Object Notation is an open standard file format,
and data interchange format, that uses human-readable text to store
and transmit data objects consisting of attribute–value pairs and array
data types (or any other serializable value). It is a very common data
format, with a diverse range of applications, such as serving as a
replacement for XML in AJAX systems.
JSON is a language-independent data format. It was derived from
JavaScript, but many modern programming languages include code
to generate and parse JSON-format data
-- https://en.wikipedia.org/wiki/JSON
{ "GNP": 8510700,
"_id": "USA",
"Name": "United States",
"IndepYear": 1776,
"geography": {
"Region": "North America",
"Continent": "North America",
"SurfaceArea": 9363520
},
"government": {
"HeadOfState": "George W. Bush",
"GovernmentForm": "Federal Republic"
},
"demographics": {
"Population": 278357000,
"LifeExpectancy": 77.0999984741211
}
}
4
What does JSON look like?
{ "GNP": 8510700,
"_id": "USA",
"Name": "United States",
"IndepYear": 1776,
"geography": {
"Region": "North America",
"Continent": "North America",
"SurfaceArea": 9363520
},
"government": {
"HeadOfState": "George W. Bush",
"GovernmentForm": "Federal Republic"
},
"demographics": {
"Population": 278357000,
"LifeExpectancy": 77.0999984741211
}
}
5
What does JSON look like?
KEY : VALUE
{ "GNP": 8510700,
"_id": "USA",
"Name": "United States",
"IndepYear": 1776,
"geography": {
"Region": "North America",
"Continent": "North America",
"SurfaceArea": 9363520
},
"government": {
"HeadOfState": "George W. Bush",
"GovernmentForm": "Federal Republic"
},
"demographics": {
"Population": 278357000,
"LifeExpectancy": 77.0999984741211
}
}
6
What does JSON look like?
Document enclosed in brackets {}
OBJECTS are enclosed in {}
JSON: OBJECTS versus ARRAYS
7
Arrays are enclosed in []
8
Pretty
much
Free Form
Strict data types
INT, CHAR, DECIMAL, etc.
Optional
Required columns
Default Values
Constraint Checks, including range
Relational Databases
9
All these features can enforce rigor on you data to
ensure that it is properly formatted BEFORE it
gets saved in the database.
BTW - Much less expensive to keep bad data out
than having to correct it later!
10
Strict
versus
Free Form
JSON’s Freeform Can hurt
How would you save email address?
email: user@foo.com
eMail: user@foo.com
e-mail: user@foo.com
electronicMail: user@foo.com
EMail: user@foo.com
eMaIl: user@foo.com
Each of the keys here are unique and valid
Searching for email addresses would require being able to look for all the forms!
11
12
Some Examples of keeping data clean
create table percona (id int, name char(25));
insert into percona values ('test');
ERROR: 1136: Column count doesn't match value count at row 1
CREATE table pz (id int, name char(25) not null);
insert into pz (id) values (1);
ERROR: 1364: Field 'name' doesn't have a default value
CREATE TABLE check_plz ( c1 int constraint c1_not_42_error check (c1 <> 42) default 42,
c2 int default 42);
Query OK, 0 rows affected (0.2902 sec)
insert into check_plz (c2) values(NULL);
ERROR: 3819: Check constraint 'c1_not_42_error' is violated.
13
But what if
there was a way
to check JSON data?
14
15
16
The Functions
JSON_SCHEMA_VALID(schema,document)
Validates a JSON document against a JSON schema.
Both schema and document are required.
The schema must be a valid JSON object; the document must be a valid JSON document.
Provided that these conditions are met:
If the document validates against the schema, the function returns true (1);
otherwise, it returns false (0).
17
The Functions
JSON_SCHEMA_VALIDATION_REPORT(schema,document)
Validates a JSON document against a JSON schema.The schema must be a valid JSON object, and the
document must be a valid JSON document. Provided that these conditions are met, the function returns a
report, as a JSON document, on the outcome of the validation. If the JSON document is considered valid
according to the JSON Schema, the function returns a JSON object with one property valid having the
value "true".
If the JSON document fails validation, the function returns a JSON object which includes the properties
listed here:
○ valid: Always "false" for a failed schema validation
○ reason: A human-readable string containing the reason for the failure
○ schema-location: A JSON pointer URI fragment identifier indicating where in the JSON
schema the validation failed (see Note following this list)
○ document-location: A JSON pointer URI fragment identifier indicating where in the JSON
document the validation failed (see Note following this list)
○ schema-failed-keyword: A string containing the name of the keyword or property in the JSON
schema that was violated
18
Simple Example 1 -- the exemplar, the new document, and the test
set @s='{"type": "object",
"properties": {
"myage": {
"type" : "number",
"minimum": 28,
"maximum": 99
}
}
}';
set @d='{ "myage": 33}'
select JSON_SCHEMA_VALID(@s,@d);
+--------------------------+
| JSON_SCHEMA_VALID(@s,@d) |
+--------------------------+
| 1 |
+--------------------------+
1 row in set (0.00 sec)
19
Simple Example 2 -- the exemplar, the new document, and the test
set @s='{"type": "object",
"properties": {
"myage": {
"type" : "number",
"minimum": 28,
"maximum": 99
}
}
}';
set @d='{ "myage": “foo”}'
select JSON_SCHEMA_VALID(@s,@d);
+--------------------------+
| JSON_SCHEMA_VALID(@s,@d) |
+--------------------------+
| 0 |
+--------------------------+
1 row in set (0.00 sec)
20
Simple Example 3 -- the exemplar, the new document, and the test
set @s='{"type": "object",
"properties": {
"myage": {
"type" : "number",
"minimum": 28,
"maximum": 99
}
}
}';
set @d='{ "myage": 16}'
select JSON_SCHEMA_VALID(@s,@d);
+--------------------------+
| JSON_SCHEMA_VALID(@s,@d) |
+--------------------------+
| 0 |
+--------------------------+
1 row in set (0.00 sec)
select JSON_PRETTY(JSON_SCHEMA_VALIDATION_REPORT(@s,@d))G
*************************** 1. row ***************************
JSON_PRETTY(JSON_SCHEMA_VALIDATION_REPORT(@s,@d)): {
"valid": false,
"reason": "The JSON document location '#/myage' failed requirement 'minimum'
at JSON Schema location '#/properties/myage'",
"schema-location": "#/properties/myage",
"document-location": "#/myage",
"schema-failed-keyword": "minimum"
}
JSON_SCHEMA_VALIDATION_REPORT()
21
CREATE TABLE `testx` (
`col` JSON,
CONSTRAINT `myage_inRange`
CHECK (JSON_SCHEMA_VALID('{"type": "object",
"properties": {
"myage": {
"type" : "number",
"minimum": 28,
"maximum": 99
}
},"required": ["myage"]
}', `col`) = 1)
);
REQUIRE Fields
22
CREATE TABLE `testx` (
`col` JSON,
CONSTRAINT `myage_inRange`
CHECK (JSON_SCHEMA_VALID('{"type": "object",
"properties": {
"myage": {
"type" : "number",
"minimum": 28,
"maximum": 99
}
},"required": ["myage"]
}', `col`) = 1)
);
REQUIRE Fields
23
insert into testx values('{"myage":27}');
ERROR 3819 (HY000):
Check constraint 'myage_inRange' is
violated.
insert into testx values('{"myage":97}');
Query OK, 1 row affected (0.02 sec)
24
❏ MySQL Manual -- https://dev.mysql.com/doc/refman/8.0/en/json-validation-functions.html
❏ Understanding JSON Schema -- http://json-schema.org/understanding-json-schema/
❏ Blog post -- https://elephantdolphin.blogspot.com/2019/07/json-schema-validation-with-mysql-
8017.html
25
Where to learn more
Copyright © 2020, Oracle and/or its affiliates | Confidential:
Internal/Restricted/Highly Restricted
26
Get $300 in credits
and try MySQL Database Service
free for 30 days.
https://www.oracle.com/cloud/free/
Test Drive MySQL Database Service For Free Today
Follow us on Social Media
27
MySQLCommunity.slack.com
Startups get cloud credits and a 70% discount for
2 years, global exposure via marketing, events,
digital promotion, and media, plus access to
mentorship, capital and Oracle’s 430,000+
customers
Customers meet vetted startups in transformative
spaces that help them stay ahead of their
competition
Oracle stays at the competitive edge
of innovation with solutions that complement its
technology stack
We have saved around 40% of our costs and are
able to reinvest that back into the business. And
we are scaling across EMEA, and that’s basically
all because of Oracle.”
—Asser Smidt
CEO and Cofounder, BotSupply
Oracle for Startups - enroll at oracle.com/startup
A Virtuous Cycle of Innovation, Everybody Wins.
28
Then please consider
buying my book on the
JSON data type, how to
use the supporting
functions, and it is filled
with example code to get
you up to speed!
Interested in using JSON with MySQL?
29
Thank You!
David.Stokes@oracle.com
@Stoker
slideshare.net/davestokes
30
Q&A
Validating JSON -- Percona Live 2021 presentation

More Related Content

PDF
Json within a relational database
PDF
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
PPTX
Discover the Power of the NoSQL + SQL with MySQL
PDF
Open Source World June '21 -- JSON Within a Relational Database
PDF
Datacon LA - MySQL without the SQL - Oh my!
PPTX
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
PDF
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
PDF
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they do
Json within a relational database
JavaScript and Friends August 20th, 20201 -- MySQL Shell and JavaScript
Discover the Power of the NoSQL + SQL with MySQL
Open Source World June '21 -- JSON Within a Relational Database
Datacon LA - MySQL without the SQL - Oh my!
Open Source 1010 and Quest InSync presentations March 30th, 2021 on MySQL Ind...
Longhorn PHP - MySQL Indexes, Histograms, Locking Options, and Other Ways to ...
Php &amp; my sql - how do pdo, mysq-li, and x devapi do what they do

What's hot (20)

PPTX
cPanel now supports MySQL 8.0 - My Top Seven Features
PDF
MySQL Replication Update - DEbconf 2020 presentation
PDF
MySQL 8.0 New Features -- September 27th presentation for Open Source Summit
PDF
MySQL 8.0 Operational Changes
PDF
Dutch PHP Conference 2021 - MySQL Indexes and Histograms
PPTX
Confoo 2021 - MySQL Indexes & Histograms
PDF
NoSQL and JavaScript: a Love Story
PDF
Developing for Node.JS with MySQL and NoSQL
PDF
My sql tutorial-oscon-2012
PDF
Lecture17
PDF
Cloudera Impala, updated for v1.0
PPTX
Using Spark to Load Oracle Data into Cassandra
PDF
Brief introduction of Slick
PPT
Real World Experience With Oracle Xml Database 11g An Oracle Ace’s Perspectiv...
PDF
20201106 hk-py con-mysql-shell
PPTX
PHP UK 2020 Tutorial: MySQL Indexes, Histograms And other ways To Speed Up Yo...
PDF
MySQL without the SQL -- Cascadia PHP
PPT
XMLDB Building Blocks And Best Practices - Oracle Open World 2008 - Marco Gra...
PDF
Using JSON with MariaDB and MySQL
PDF
Simple Jdbc With Spring 2.5
cPanel now supports MySQL 8.0 - My Top Seven Features
MySQL Replication Update - DEbconf 2020 presentation
MySQL 8.0 New Features -- September 27th presentation for Open Source Summit
MySQL 8.0 Operational Changes
Dutch PHP Conference 2021 - MySQL Indexes and Histograms
Confoo 2021 - MySQL Indexes & Histograms
NoSQL and JavaScript: a Love Story
Developing for Node.JS with MySQL and NoSQL
My sql tutorial-oscon-2012
Lecture17
Cloudera Impala, updated for v1.0
Using Spark to Load Oracle Data into Cassandra
Brief introduction of Slick
Real World Experience With Oracle Xml Database 11g An Oracle Ace’s Perspectiv...
20201106 hk-py con-mysql-shell
PHP UK 2020 Tutorial: MySQL Indexes, Histograms And other ways To Speed Up Yo...
MySQL without the SQL -- Cascadia PHP
XMLDB Building Blocks And Best Practices - Oracle Open World 2008 - Marco Gra...
Using JSON with MariaDB and MySQL
Simple Jdbc With Spring 2.5
Ad

Similar to Validating JSON -- Percona Live 2021 presentation (20)

PDF
UKOUG Tech14 - Getting Started With JSON in the Database
PDF
Store non-structured data in JSON column types and enhancements of JSON
PPTX
Oracle Database - JSON and the In-Memory Database
PPTX
Starting with JSON Path Expressions in Oracle 12.1.0.2
PPT
json.ppt download for free for college project
PPTX
The rise of json in rdbms land jab17
PDF
Json at work overview and ecosystem-v2.0
PDF
Going Native: Leveraging the New JSON Native Datatype in Oracle 21c
PPTX
Introduction to JSON & AJAX
PDF
Json
PPT
Advanced Json
PDF
Json the-x-in-ajax1588
PPTX
Tk2323 lecture 9 api json
PDF
JSON Data Parsing in Snowflake (By Faysal Shaarani)
PDF
Os Pruett
PDF
Basics of JSON (JavaScript Object Notation) with examples
PPT
jQuery Datatables With MongDb
PDF
Opa presentation at GamesJs
UKOUG Tech14 - Getting Started With JSON in the Database
Store non-structured data in JSON column types and enhancements of JSON
Oracle Database - JSON and the In-Memory Database
Starting with JSON Path Expressions in Oracle 12.1.0.2
json.ppt download for free for college project
The rise of json in rdbms land jab17
Json at work overview and ecosystem-v2.0
Going Native: Leveraging the New JSON Native Datatype in Oracle 21c
Introduction to JSON & AJAX
Json
Advanced Json
Json the-x-in-ajax1588
Tk2323 lecture 9 api json
JSON Data Parsing in Snowflake (By Faysal Shaarani)
Os Pruett
Basics of JSON (JavaScript Object Notation) with examples
jQuery Datatables With MongDb
Opa presentation at GamesJs
Ad

More from Dave Stokes (15)

PDF
Database basics for new-ish developers -- All Things Open October 18th 2021
PDF
Midwest PHP Presentation - New MSQL Features
PDF
Data Love Conference - Window Functions for Database Analytics
PPTX
Confoo 2021 -- MySQL New Features
PPTX
A Step by Step Introduction to the MySQL Document Store
PPTX
Discover The Power of NoSQL + MySQL with MySQL
PDF
Confoo 202 - MySQL Group Replication and ReplicaSet
PDF
MySQL New Features -- Sunshine PHP 2020 Presentation
PPTX
MySQL 8.0 from December London Open Source Database Meetup
PPTX
MySQL 8 - UKOUG Techfest Brighton December 2nd, 2019
PPTX
Upgrading to MySQL 8.0 webinar slides November 27th, 2019
PDF
Windowing Functions - Little Rock Tech Fest 2019
PDF
Oracle CodeOne Foreign Keys Support in MySQL 8.0
PDF
MySQL Without the SQL - Oh My! August 2nd presentation at Mid Atlantic Develo...
PDF
MySQL 8.0 Graphical Information System - Mid Atlantic Developers Conference
Database basics for new-ish developers -- All Things Open October 18th 2021
Midwest PHP Presentation - New MSQL Features
Data Love Conference - Window Functions for Database Analytics
Confoo 2021 -- MySQL New Features
A Step by Step Introduction to the MySQL Document Store
Discover The Power of NoSQL + MySQL with MySQL
Confoo 202 - MySQL Group Replication and ReplicaSet
MySQL New Features -- Sunshine PHP 2020 Presentation
MySQL 8.0 from December London Open Source Database Meetup
MySQL 8 - UKOUG Techfest Brighton December 2nd, 2019
Upgrading to MySQL 8.0 webinar slides November 27th, 2019
Windowing Functions - Little Rock Tech Fest 2019
Oracle CodeOne Foreign Keys Support in MySQL 8.0
MySQL Without the SQL - Oh My! August 2nd presentation at Mid Atlantic Develo...
MySQL 8.0 Graphical Information System - Mid Atlantic Developers Conference

Recently uploaded (20)

PDF
The Internet -By the Numbers, Sri Lanka Edition
PPTX
Internet___Basics___Styled_ presentation
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
PDF
RPKI Status Update, presented by Makito Lay at IDNOG 10
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PPTX
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
PDF
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
PDF
Cloud-Scale Log Monitoring _ Datadog.pdf
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
PDF
Sims 4 Historia para lo sims 4 para jugar
PPTX
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
DOCX
Unit-3 cyber security network security of internet system
PDF
Triggering QUIC, presented by Geoff Huston at IETF 123
PDF
Slides PDF The World Game (s) Eco Economic Epochs.pdf
PPTX
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PDF
Paper PDF World Game (s) Great Redesign.pdf
PDF
WebRTC in SignalWire - troubleshooting media negotiation
The Internet -By the Numbers, Sri Lanka Edition
Internet___Basics___Styled_ presentation
Module 1 - Cyber Law and Ethics 101.pptx
RPKI Status Update, presented by Makito Lay at IDNOG 10
Design_with_Watersergyerge45hrbgre4top (1).ppt
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
522797556-Unit-2-Temperature-measurement-1-1.pptx
Vigrab.top – Online Tool for Downloading and Converting Social Media Videos a...
Cloud-Scale Log Monitoring _ Datadog.pdf
The New Creative Director: How AI Tools for Social Media Content Creation Are...
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
Sims 4 Historia para lo sims 4 para jugar
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
Unit-3 cyber security network security of internet system
Triggering QUIC, presented by Geoff Huston at IETF 123
Slides PDF The World Game (s) Eco Economic Epochs.pdf
June-4-Sermon-Powerpoint.pptx USE THIS FOR YOUR MOTIVATION
Introuction about WHO-FIC in ICD-10.pptx
Paper PDF World Game (s) Great Redesign.pdf
WebRTC in SignalWire - troubleshooting media negotiation

Validating JSON -- Percona Live 2021 presentation

  • 1. JSON Validation Dave Stokes MySQL Community Manager MySQL Community Team
  • 2. Dave Stokes MySQL Community Team Oracle Corporation @Stoker https://elephantdolphin.blogspot.com/ David.Stokes@Oracle.com Slides are available at Slideshare.net/davestokes 2
  • 3. 3 JSON JSON (JavaScript Object Notation is an open standard file format, and data interchange format, that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and array data types (or any other serializable value). It is a very common data format, with a diverse range of applications, such as serving as a replacement for XML in AJAX systems. JSON is a language-independent data format. It was derived from JavaScript, but many modern programming languages include code to generate and parse JSON-format data -- https://en.wikipedia.org/wiki/JSON
  • 4. { "GNP": 8510700, "_id": "USA", "Name": "United States", "IndepYear": 1776, "geography": { "Region": "North America", "Continent": "North America", "SurfaceArea": 9363520 }, "government": { "HeadOfState": "George W. Bush", "GovernmentForm": "Federal Republic" }, "demographics": { "Population": 278357000, "LifeExpectancy": 77.0999984741211 } } 4 What does JSON look like?
  • 5. { "GNP": 8510700, "_id": "USA", "Name": "United States", "IndepYear": 1776, "geography": { "Region": "North America", "Continent": "North America", "SurfaceArea": 9363520 }, "government": { "HeadOfState": "George W. Bush", "GovernmentForm": "Federal Republic" }, "demographics": { "Population": 278357000, "LifeExpectancy": 77.0999984741211 } } 5 What does JSON look like? KEY : VALUE
  • 6. { "GNP": 8510700, "_id": "USA", "Name": "United States", "IndepYear": 1776, "geography": { "Region": "North America", "Continent": "North America", "SurfaceArea": 9363520 }, "government": { "HeadOfState": "George W. Bush", "GovernmentForm": "Federal Republic" }, "demographics": { "Population": 278357000, "LifeExpectancy": 77.0999984741211 } } 6 What does JSON look like? Document enclosed in brackets {}
  • 7. OBJECTS are enclosed in {} JSON: OBJECTS versus ARRAYS 7 Arrays are enclosed in []
  • 9. Strict data types INT, CHAR, DECIMAL, etc. Optional Required columns Default Values Constraint Checks, including range Relational Databases 9 All these features can enforce rigor on you data to ensure that it is properly formatted BEFORE it gets saved in the database. BTW - Much less expensive to keep bad data out than having to correct it later!
  • 11. JSON’s Freeform Can hurt How would you save email address? email: user@foo.com eMail: user@foo.com e-mail: user@foo.com electronicMail: user@foo.com EMail: user@foo.com eMaIl: user@foo.com Each of the keys here are unique and valid Searching for email addresses would require being able to look for all the forms! 11
  • 12. 12 Some Examples of keeping data clean create table percona (id int, name char(25)); insert into percona values ('test'); ERROR: 1136: Column count doesn't match value count at row 1 CREATE table pz (id int, name char(25) not null); insert into pz (id) values (1); ERROR: 1364: Field 'name' doesn't have a default value CREATE TABLE check_plz ( c1 int constraint c1_not_42_error check (c1 <> 42) default 42, c2 int default 42); Query OK, 0 rows affected (0.2902 sec) insert into check_plz (c2) values(NULL); ERROR: 3819: Check constraint 'c1_not_42_error' is violated.
  • 13. 13 But what if there was a way to check JSON data?
  • 14. 14
  • 15. 15
  • 16. 16 The Functions JSON_SCHEMA_VALID(schema,document) Validates a JSON document against a JSON schema. Both schema and document are required. The schema must be a valid JSON object; the document must be a valid JSON document. Provided that these conditions are met: If the document validates against the schema, the function returns true (1); otherwise, it returns false (0).
  • 17. 17 The Functions JSON_SCHEMA_VALIDATION_REPORT(schema,document) Validates a JSON document against a JSON schema.The schema must be a valid JSON object, and the document must be a valid JSON document. Provided that these conditions are met, the function returns a report, as a JSON document, on the outcome of the validation. If the JSON document is considered valid according to the JSON Schema, the function returns a JSON object with one property valid having the value "true". If the JSON document fails validation, the function returns a JSON object which includes the properties listed here: ○ valid: Always "false" for a failed schema validation ○ reason: A human-readable string containing the reason for the failure ○ schema-location: A JSON pointer URI fragment identifier indicating where in the JSON schema the validation failed (see Note following this list) ○ document-location: A JSON pointer URI fragment identifier indicating where in the JSON document the validation failed (see Note following this list) ○ schema-failed-keyword: A string containing the name of the keyword or property in the JSON schema that was violated
  • 18. 18 Simple Example 1 -- the exemplar, the new document, and the test set @s='{"type": "object", "properties": { "myage": { "type" : "number", "minimum": 28, "maximum": 99 } } }'; set @d='{ "myage": 33}' select JSON_SCHEMA_VALID(@s,@d); +--------------------------+ | JSON_SCHEMA_VALID(@s,@d) | +--------------------------+ | 1 | +--------------------------+ 1 row in set (0.00 sec)
  • 19. 19 Simple Example 2 -- the exemplar, the new document, and the test set @s='{"type": "object", "properties": { "myage": { "type" : "number", "minimum": 28, "maximum": 99 } } }'; set @d='{ "myage": “foo”}' select JSON_SCHEMA_VALID(@s,@d); +--------------------------+ | JSON_SCHEMA_VALID(@s,@d) | +--------------------------+ | 0 | +--------------------------+ 1 row in set (0.00 sec)
  • 20. 20 Simple Example 3 -- the exemplar, the new document, and the test set @s='{"type": "object", "properties": { "myage": { "type" : "number", "minimum": 28, "maximum": 99 } } }'; set @d='{ "myage": 16}' select JSON_SCHEMA_VALID(@s,@d); +--------------------------+ | JSON_SCHEMA_VALID(@s,@d) | +--------------------------+ | 0 | +--------------------------+ 1 row in set (0.00 sec)
  • 21. select JSON_PRETTY(JSON_SCHEMA_VALIDATION_REPORT(@s,@d))G *************************** 1. row *************************** JSON_PRETTY(JSON_SCHEMA_VALIDATION_REPORT(@s,@d)): { "valid": false, "reason": "The JSON document location '#/myage' failed requirement 'minimum' at JSON Schema location '#/properties/myage'", "schema-location": "#/properties/myage", "document-location": "#/myage", "schema-failed-keyword": "minimum" } JSON_SCHEMA_VALIDATION_REPORT() 21
  • 22. CREATE TABLE `testx` ( `col` JSON, CONSTRAINT `myage_inRange` CHECK (JSON_SCHEMA_VALID('{"type": "object", "properties": { "myage": { "type" : "number", "minimum": 28, "maximum": 99 } },"required": ["myage"] }', `col`) = 1) ); REQUIRE Fields 22
  • 23. CREATE TABLE `testx` ( `col` JSON, CONSTRAINT `myage_inRange` CHECK (JSON_SCHEMA_VALID('{"type": "object", "properties": { "myage": { "type" : "number", "minimum": 28, "maximum": 99 } },"required": ["myage"] }', `col`) = 1) ); REQUIRE Fields 23 insert into testx values('{"myage":27}'); ERROR 3819 (HY000): Check constraint 'myage_inRange' is violated. insert into testx values('{"myage":97}'); Query OK, 1 row affected (0.02 sec)
  • 24. 24
  • 25. ❏ MySQL Manual -- https://dev.mysql.com/doc/refman/8.0/en/json-validation-functions.html ❏ Understanding JSON Schema -- http://json-schema.org/understanding-json-schema/ ❏ Blog post -- https://elephantdolphin.blogspot.com/2019/07/json-schema-validation-with-mysql- 8017.html 25 Where to learn more
  • 26. Copyright © 2020, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 26 Get $300 in credits and try MySQL Database Service free for 30 days. https://www.oracle.com/cloud/free/ Test Drive MySQL Database Service For Free Today
  • 27. Follow us on Social Media 27 MySQLCommunity.slack.com
  • 28. Startups get cloud credits and a 70% discount for 2 years, global exposure via marketing, events, digital promotion, and media, plus access to mentorship, capital and Oracle’s 430,000+ customers Customers meet vetted startups in transformative spaces that help them stay ahead of their competition Oracle stays at the competitive edge of innovation with solutions that complement its technology stack We have saved around 40% of our costs and are able to reinvest that back into the business. And we are scaling across EMEA, and that’s basically all because of Oracle.” —Asser Smidt CEO and Cofounder, BotSupply Oracle for Startups - enroll at oracle.com/startup A Virtuous Cycle of Innovation, Everybody Wins. 28
  • 29. Then please consider buying my book on the JSON data type, how to use the supporting functions, and it is filled with example code to get you up to speed! Interested in using JSON with MySQL? 29