Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
JSON_TABLE
Øystein Grøvlen
Optimizer Geek
Oracle
April 24, 2018
The Best of Both Worlds
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
JSON Data Type
CREATE TABLE t1(json_col JSON);
INSERT INTO t1 VALUES (
'{ "people": [
{ "name":"John Smith", "address":"780 Mission St, San Francisco, CA 94103"},
{ "name":"Sally Brown", "address":"75 37th Ave S, St Cloud, MN 94103"},
{ "name":"John Johnson", "address":"1262 Roosevelt Trail, Raymond, ME 04071"}
] }'
);
2
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
JSON_TABLE
SELECT people.*
FROM t1,
JSON_TABLE(json_col, '$.people[*]' COLUMNS (
name VARCHAR(40) PATH '$.name',
address VARCHAR(100) PATH '$.address')) people;
3
Convert JSON documents to relational tables
name address
John Smith 780 Mission St, San Francisco, CA 94103
Sally Brown 75 37th Ave S, St Cloud, MN 9410
John Johnson 1262 Roosevelt Trail, Raymond, ME 04071
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
JSON_TABLE
SELECT people.*
FROM t1,
JSON_TABLE(json_col, '$.people[*]' COLUMNS (
name VARCHAR(40) PATH '$.name',
address VARCHAR(100) PATH '$.address')) people;
WHERE people.name LIKE 'John%';
4
Filter JSON data
name address
John Smith 780 Mission St, San Francisco, CA 94103
John Johnson 1262 Roosevelt Trail, Raymond, ME 04071
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Construct a New JSON Document
SELECT JSON_OBJECT("people",
JSON_ARRAYAGG(JSON_OBJECT("name", name, "address", address))) json_doc
FROM t1,
JSON_TABLE(json_col, '$.people[*]' COLUMNS (
name VARCHAR(40) PATH '$.name',
address VARCHAR(100) PATH '$.address')) people;
WHERE people.name LIKE 'John%';
5
json_doc
{"people": [{"name": "John Smith", "address": "780 Mission St, San Francisco,
CA 94103"}, {"name": "John Johnson", "address": "1262 Roosevelt Trail,
Raymond, ME 04071"}]}
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | 6
JSON_TABLE
id father married child_id child age
1 John 1 1 Eric 12
1 John 1 2 Beth 10
2 Paul 0 1 Sarah 9
2 Paul 0 2 Noah 3
2 Paul 0 3 Peter 1
[
{ "father":"John", "mother":"Mary",
"marriage_date":"2003-12-05",
"children": [
{ "name":"Eric", "age":12 },
{ "name":"Beth", "age":10 } ] },
{ "father":"Paul", "mother":"Laura",
"children": [
{ "name":"Sarah", "age":9},
{ "name":"Noah", "age":3} ,
{ "name":"Peter", "age":1} ] }
]
Nested arrays
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | 7
JSON_TABLE – Nested Arrays
id father married child_id child age
1 John 1 1 Eric 12
1 John 1 2 Beth 10
2 Paul 0 1 Sarah 9
2 Paul 0 2 Noah 3
2 Paul 0 3 Peter 1
JSON_TABLE (families, '$[*]' COLUMNS (
id FOR ORDINALITY,
father VARCHAR(30) PATH '$.father',
married INTEGER EXISTS PATH
'$.marriage_date',
NESTED PATH '$.children[*]' COLUMNS (
child_id FOR ORDINALITY,
child VARCHAR(30) PATH '$.name',
age INTEGER PATH '$.age') ) )
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
JSON_TABLE
SELECT father, COUNT(*) "#children", AVG(age) "age average"
FROM t, JSON_TABLE (families, '$[*]' COLUMNS (
id FOR ORDINALITY,
father VARCHAR(30) PATH '$.father',
NESTED PATH '$.children[*]' COLUMNS (age INTEGER PATH '$.age' ) ) ) AS fam
GROUP BY id, father;
8
SQL aggregation on JSON data
father #children age average
John 2 11.0000
Paul 3 4.3333
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Put Computed Data Back Into JSON Document
SELECT JSON_ARRAYAGG(fam_obj) families
FROM (
SELECT JSON_MERGE_PATCH(family,
JSON_OBJECT("#children", COUNT(*), "avg_age" , AVG(age))) fam_obj
FROM t, JSON_TABLE (families, '$[*]' COLUMNS (
id FOR ORDINALITY,
family JSON PATH '$',
NESTED PATH '$.children[*]' COLUMNS (age INTEGER PATH '$.age' ) )
) fam
GROUP BY id, family) fams;
9
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Modify JSON Objects
SELECT
JSON_ARRAYAGG(JSON_MERGE_PATCH(JSON_OBJECT("id", id), family))
AS families
FROM t, JSON_TABLE (families, '$[*]' COLUMNS (
id FOR ORDINALITY,
family JSON PATH '$')) fam;
10
Put IDs in objects
families
[{"id": 1, "father": "John", "mother": "Mary", "children": [{"age": 12, "name":
"Eric"}, {"age": 10, "name": "Beth"}], "marriage_date": "2003-12-05"},
{"id": 2, "father": "Paul", "mother": "Laura", "children": [{"age": 9, "name":
"Sarah"}, {"age": 3, "name": "Noah"}, {"age": 1, "name": "Peter"}]}]
Copyright © 2018, Oracle and/or its affiliates. All rights reserved. |
Want to Learn More About New and Upcoming SQL
Features?
• MySQL 8.0: What is New in Optimizer and Executor?
– Manyi Lu
– Tuesday 4:50PM-5:15PM; Room GS
• Running JavaScript Stored-Programs Inside MySQL Server
– Øystein Grøvlen, Vojin Jovanovic, Farhan Tauheed
– Wednesday 11:00AM-11:50AM; Room GS
11
Presentations at Percona Live

More Related Content

PDF
MongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
PPTX
How to Win Friends and Influence People (with Hadoop)
PDF
MongoDB Europe 2016 - ETL for Pros – Getting Data Into MongoDB The Right Way
PPTX
PHP Database Programming Basics -- Northeast PHP
PPTX
ETL for Pros: Getting Data Into MongoDB
PDF
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
PDF
MongoDB .local Toronto 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...
PDF
MongoDB .local Chicago 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...
MongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
How to Win Friends and Influence People (with Hadoop)
MongoDB Europe 2016 - ETL for Pros – Getting Data Into MongoDB The Right Way
PHP Database Programming Basics -- Northeast PHP
ETL for Pros: Getting Data Into MongoDB
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
MongoDB .local Toronto 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...
MongoDB .local Chicago 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...

Similar to JSON_TABLE -- The best of both worlds (20)

PDF
More SQL in MySQL 8.0
PDF
MySQL's JSON Data Type and Document Store
PPTX
PostgreSQL 9.4 JSON Types and Operators
PDF
MySQL 5.7 + JSON
PPTX
BGOUG15: JSON support in MySQL 5.7
PDF
MySQL JSON Functions
PPTX
Starting with JSON Path Expressions in Oracle 12.1.0.2
PDF
UKOUG Tech14 - Getting Started With JSON in the Database
PDF
JSON Array Indexes in MySQL
PDF
JSON Support in MariaDB: News, non-news and the bigger picture
PDF
Optimizer percona live_ams2015
PDF
Second Step to the NoSQL Side: MySQL JSON Functions
PDF
JSON Data Parsing in Snowflake (By Faysal Shaarani)
PPTX
The rise of json in rdbms land jab17
PPTX
MySQL Rises with JSON Support
PPTX
Oracle Database - JSON and the In-Memory Database
PPSX
JSON, A Splash of SODA, and a SQL Chaser: Real-World Use Cases for Autonomous...
PDF
JSON Processing in the Database using PostgreSQL 9.4 :: Data Wranglers DC :: ...
PPT
Using JSON/BSON types in your hybrid application environment
PDF
JSON array indexes in MySQL
More SQL in MySQL 8.0
MySQL's JSON Data Type and Document Store
PostgreSQL 9.4 JSON Types and Operators
MySQL 5.7 + JSON
BGOUG15: JSON support in MySQL 5.7
MySQL JSON Functions
Starting with JSON Path Expressions in Oracle 12.1.0.2
UKOUG Tech14 - Getting Started With JSON in the Database
JSON Array Indexes in MySQL
JSON Support in MariaDB: News, non-news and the bigger picture
Optimizer percona live_ams2015
Second Step to the NoSQL Side: MySQL JSON Functions
JSON Data Parsing in Snowflake (By Faysal Shaarani)
The rise of json in rdbms land jab17
MySQL Rises with JSON Support
Oracle Database - JSON and the In-Memory Database
JSON, A Splash of SODA, and a SQL Chaser: Real-World Use Cases for Autonomous...
JSON Processing in the Database using PostgreSQL 9.4 :: Data Wranglers DC :: ...
Using JSON/BSON types in your hybrid application environment
JSON array indexes in MySQL
Ad

More from oysteing (17)

PDF
POLARDB: A database architecture for the cloud
PDF
The MySQL Query Optimizer Explained Through Optimizer Trace
PDF
POLARDB: A database architecture for the cloud
PDF
POLARDB for MySQL - Parallel Query
PDF
Histogram Support in MySQL 8.0
PDF
MySQL Optimizer: What’s New in 8.0
PDF
How to Analyze and Tune MySQL Queries for Better Performance
PDF
Common Table Expressions (CTE) & Window Functions in MySQL 8.0
PDF
How to analyze and tune sql queries for better performance
PDF
Using Optimizer Hints to Improve MySQL Query Performance
PDF
MySQL 8.0: Common Table Expressions
PDF
How to Analyze and Tune MySQL Queries for Better Performance
PDF
MySQL 8.0: Common Table Expressions
PDF
How to analyze and tune sql queries for better performance vts2016
PDF
How to Analyze and Tune MySQL Queries for Better Performance
PDF
How to analyze and tune sql queries for better performance percona15
PDF
How to analyze and tune sql queries for better performance webinar
POLARDB: A database architecture for the cloud
The MySQL Query Optimizer Explained Through Optimizer Trace
POLARDB: A database architecture for the cloud
POLARDB for MySQL - Parallel Query
Histogram Support in MySQL 8.0
MySQL Optimizer: What’s New in 8.0
How to Analyze and Tune MySQL Queries for Better Performance
Common Table Expressions (CTE) & Window Functions in MySQL 8.0
How to analyze and tune sql queries for better performance
Using Optimizer Hints to Improve MySQL Query Performance
MySQL 8.0: Common Table Expressions
How to Analyze and Tune MySQL Queries for Better Performance
MySQL 8.0: Common Table Expressions
How to analyze and tune sql queries for better performance vts2016
How to Analyze and Tune MySQL Queries for Better Performance
How to analyze and tune sql queries for better performance percona15
How to analyze and tune sql queries for better performance webinar
Ad

Recently uploaded (20)

PDF
Data Engineering Interview Questions & Answers Data Modeling (3NF, Star, Vaul...
PPTX
chrmotography.pptx food anaylysis techni
PDF
Optimise Shopper Experiences with a Strong Data Estate.pdf
PPTX
Topic 5 Presentation 5 Lesson 5 Corporate Fin
PPTX
SET 1 Compulsory MNH machine learning intro
PDF
Votre score augmente si vous choisissez une catégorie et que vous rédigez une...
PPTX
CYBER SECURITY the Next Warefare Tactics
PPT
lectureusjsjdhdsjjshdshshddhdhddhhd1.ppt
PPT
Image processing and pattern recognition 2.ppt
PPTX
A Complete Guide to Streamlining Business Processes
PPTX
modul_python (1).pptx for professional and student
PPTX
DS-40-Pre-Engagement and Kickoff deck - v8.0.pptx
PPTX
STERILIZATION AND DISINFECTION-1.ppthhhbx
PDF
Tetra Pak Index 2023 - The future of health and nutrition - Full report.pdf
PDF
Transcultural that can help you someday.
DOCX
Factor Analysis Word Document Presentation
PDF
REAL ILLUMINATI AGENT IN KAMPALA UGANDA CALL ON+256765750853/0705037305
PPTX
Copy of 16 Timeline & Flowchart Templates – HubSpot.pptx
PPTX
FMIS 108 and AISlaudon_mis17_ppt_ch11.pptx
PPTX
New ISO 27001_2022 standard and the changes
Data Engineering Interview Questions & Answers Data Modeling (3NF, Star, Vaul...
chrmotography.pptx food anaylysis techni
Optimise Shopper Experiences with a Strong Data Estate.pdf
Topic 5 Presentation 5 Lesson 5 Corporate Fin
SET 1 Compulsory MNH machine learning intro
Votre score augmente si vous choisissez une catégorie et que vous rédigez une...
CYBER SECURITY the Next Warefare Tactics
lectureusjsjdhdsjjshdshshddhdhddhhd1.ppt
Image processing and pattern recognition 2.ppt
A Complete Guide to Streamlining Business Processes
modul_python (1).pptx for professional and student
DS-40-Pre-Engagement and Kickoff deck - v8.0.pptx
STERILIZATION AND DISINFECTION-1.ppthhhbx
Tetra Pak Index 2023 - The future of health and nutrition - Full report.pdf
Transcultural that can help you someday.
Factor Analysis Word Document Presentation
REAL ILLUMINATI AGENT IN KAMPALA UGANDA CALL ON+256765750853/0705037305
Copy of 16 Timeline & Flowchart Templates – HubSpot.pptx
FMIS 108 and AISlaudon_mis17_ppt_ch11.pptx
New ISO 27001_2022 standard and the changes

JSON_TABLE -- The best of both worlds

  • 1. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | JSON_TABLE Øystein Grøvlen Optimizer Geek Oracle April 24, 2018 The Best of Both Worlds
  • 2. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | JSON Data Type CREATE TABLE t1(json_col JSON); INSERT INTO t1 VALUES ( '{ "people": [ { "name":"John Smith", "address":"780 Mission St, San Francisco, CA 94103"}, { "name":"Sally Brown", "address":"75 37th Ave S, St Cloud, MN 94103"}, { "name":"John Johnson", "address":"1262 Roosevelt Trail, Raymond, ME 04071"} ] }' ); 2
  • 3. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | JSON_TABLE SELECT people.* FROM t1, JSON_TABLE(json_col, '$.people[*]' COLUMNS ( name VARCHAR(40) PATH '$.name', address VARCHAR(100) PATH '$.address')) people; 3 Convert JSON documents to relational tables name address John Smith 780 Mission St, San Francisco, CA 94103 Sally Brown 75 37th Ave S, St Cloud, MN 9410 John Johnson 1262 Roosevelt Trail, Raymond, ME 04071
  • 4. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | JSON_TABLE SELECT people.* FROM t1, JSON_TABLE(json_col, '$.people[*]' COLUMNS ( name VARCHAR(40) PATH '$.name', address VARCHAR(100) PATH '$.address')) people; WHERE people.name LIKE 'John%'; 4 Filter JSON data name address John Smith 780 Mission St, San Francisco, CA 94103 John Johnson 1262 Roosevelt Trail, Raymond, ME 04071
  • 5. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Construct a New JSON Document SELECT JSON_OBJECT("people", JSON_ARRAYAGG(JSON_OBJECT("name", name, "address", address))) json_doc FROM t1, JSON_TABLE(json_col, '$.people[*]' COLUMNS ( name VARCHAR(40) PATH '$.name', address VARCHAR(100) PATH '$.address')) people; WHERE people.name LIKE 'John%'; 5 json_doc {"people": [{"name": "John Smith", "address": "780 Mission St, San Francisco, CA 94103"}, {"name": "John Johnson", "address": "1262 Roosevelt Trail, Raymond, ME 04071"}]}
  • 6. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | 6 JSON_TABLE id father married child_id child age 1 John 1 1 Eric 12 1 John 1 2 Beth 10 2 Paul 0 1 Sarah 9 2 Paul 0 2 Noah 3 2 Paul 0 3 Peter 1 [ { "father":"John", "mother":"Mary", "marriage_date":"2003-12-05", "children": [ { "name":"Eric", "age":12 }, { "name":"Beth", "age":10 } ] }, { "father":"Paul", "mother":"Laura", "children": [ { "name":"Sarah", "age":9}, { "name":"Noah", "age":3} , { "name":"Peter", "age":1} ] } ] Nested arrays
  • 7. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | 7 JSON_TABLE – Nested Arrays id father married child_id child age 1 John 1 1 Eric 12 1 John 1 2 Beth 10 2 Paul 0 1 Sarah 9 2 Paul 0 2 Noah 3 2 Paul 0 3 Peter 1 JSON_TABLE (families, '$[*]' COLUMNS ( id FOR ORDINALITY, father VARCHAR(30) PATH '$.father', married INTEGER EXISTS PATH '$.marriage_date', NESTED PATH '$.children[*]' COLUMNS ( child_id FOR ORDINALITY, child VARCHAR(30) PATH '$.name', age INTEGER PATH '$.age') ) )
  • 8. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | JSON_TABLE SELECT father, COUNT(*) "#children", AVG(age) "age average" FROM t, JSON_TABLE (families, '$[*]' COLUMNS ( id FOR ORDINALITY, father VARCHAR(30) PATH '$.father', NESTED PATH '$.children[*]' COLUMNS (age INTEGER PATH '$.age' ) ) ) AS fam GROUP BY id, father; 8 SQL aggregation on JSON data father #children age average John 2 11.0000 Paul 3 4.3333
  • 9. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Put Computed Data Back Into JSON Document SELECT JSON_ARRAYAGG(fam_obj) families FROM ( SELECT JSON_MERGE_PATCH(family, JSON_OBJECT("#children", COUNT(*), "avg_age" , AVG(age))) fam_obj FROM t, JSON_TABLE (families, '$[*]' COLUMNS ( id FOR ORDINALITY, family JSON PATH '$', NESTED PATH '$.children[*]' COLUMNS (age INTEGER PATH '$.age' ) ) ) fam GROUP BY id, family) fams; 9
  • 10. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Modify JSON Objects SELECT JSON_ARRAYAGG(JSON_MERGE_PATCH(JSON_OBJECT("id", id), family)) AS families FROM t, JSON_TABLE (families, '$[*]' COLUMNS ( id FOR ORDINALITY, family JSON PATH '$')) fam; 10 Put IDs in objects families [{"id": 1, "father": "John", "mother": "Mary", "children": [{"age": 12, "name": "Eric"}, {"age": 10, "name": "Beth"}], "marriage_date": "2003-12-05"}, {"id": 2, "father": "Paul", "mother": "Laura", "children": [{"age": 9, "name": "Sarah"}, {"age": 3, "name": "Noah"}, {"age": 1, "name": "Peter"}]}]
  • 11. Copyright © 2018, Oracle and/or its affiliates. All rights reserved. | Want to Learn More About New and Upcoming SQL Features? • MySQL 8.0: What is New in Optimizer and Executor? – Manyi Lu – Tuesday 4:50PM-5:15PM; Room GS • Running JavaScript Stored-Programs Inside MySQL Server – Øystein Grøvlen, Vojin Jovanovic, Farhan Tauheed – Wednesday 11:00AM-11:50AM; Room GS 11 Presentations at Percona Live