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

Validating JSON -- Percona Live 2021 presentation

  • 1.
    JSON Validation Dave Stokes MySQL CommunityManager MySQL Community Team
  • 2.
    Dave Stokes MySQL CommunityTeam Oracle Corporation @Stoker https://elephantdolphin.blogspot.com/ David.Stokes@Oracle.com Slides are available at Slideshare.net/davestokes 2
  • 3.
    3 JSON JSON (JavaScript ObjectNotation 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 enclosedin {} JSON: OBJECTS versus ARRAYS 7 Arrays are enclosed in []
  • 8.
  • 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!
  • 10.
  • 11.
    JSON’s Freeform Canhurt 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 ofkeeping 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 therewas a way to check JSON data?
  • 14.
  • 15.
  • 16.
    16 The Functions JSON_SCHEMA_VALID(schema,document) Validates aJSON 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 aJSON 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.
  • 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 onSocial Media 27 MySQLCommunity.slack.com
  • 28.
    Startups get cloudcredits 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 buyingmy 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
  • 30.