DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Introduction to Couchbase for Oracle Developers and Experts: Part 2 - Database Objects
  • Designing a Blog Application Using Document Databases
  • Keep Calm and Column Wise
  • Migrating MuleSoft System API to AWS Lambda (Part 1)

Trending

  • How AI Agents Are Transforming Enterprise Automation Architecture
  • 5 Subtle Indicators Your Development Environment Is Under Siege
  • Build Your First AI Model in Python: A Beginner's Guide (1 of 3)
  • Teradata Performance and Skew Prevention Tips
  1. DZone
  2. Data Engineering
  3. Databases
  4. JSON and PL/SQL 12.2: Get Values From JSON Documents With API

JSON and PL/SQL 12.2: Get Values From JSON Documents With API

Learn about pre-defined PL/SQL object types to perform fine-grained programmatic construction and manipulation of in-memory JSON data.

By 
Steven Feuerstein user avatar
Steven Feuerstein
·
Jan. 26, 18 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
13.1K Views

Join the DZone community and get the full member experience.

Join For Free

With the release of Oracle Database 12c, Oracle SQL entered the Age of JSON. You can use SQL to join JSON data with relational data. You can extract content from within the JSON document and make it available for relational processes and tools. You can even query, right from within the database, JSON data that is stored outside Oracle Database in an external table. Check out my post on JSON resources for more guidance on all of the above.

And that was all possible in 12.1. In Oracle Database 12c Release 2, we added several pre-defined PL/SQL object types to perform fine-grained programmatic construction and manipulation of in-memory JSON data. I'll publish occasional posts on what you can do with these types. Here's the first one, showing you how to get the value for a specific key in a JSON document.

Suppose I've created and populated my species table as follows:

CREATE TABLE json_species
(
   id     NUMBER NOT NULL PRIMARY KEY,
   info   CLOB CONSTRAINT is_json CHECK (info IS JSON ) )
/

BEGIN
   INSERT INTO json_species
        VALUES (1, '{"name":"Spider"}');

   INSERT INTO json_species
        VALUES (2, '{"name":"Elephant", "trunk_length":"10"}');

   INSERT INTO json_species
        VALUES (3, '{"name":"Shark", "fin_count":"4"}');

   COMMIT;
END;
/

If I want to see the values for names in the JSON documents in this table, I can use SQL:

SELECT sp.info.name
  FROM json_species sp

And if this is all you need, there's no reason to bring in the PL/SQL types. If, however, you need to get values in order to perform additional processing on them and perhaps even change the values, the PL/SQL API comes in handy.

Here's how I can get those same values (and display them) in PL/SQL:

DECLARE
   CURSOR species_cur
   IS
      SELECT id, sp.info
        FROM json_species sp
       ORDER BY id;

   l_species   json_object_t;
   l_name      json_element_t;
BEGIN
   FOR rec IN species_cur
   LOOP
      l_species := json_object_t (rec.info);
      l_name := l_species.get ('name');
      DBMS_OUTPUT.put_line (l_name.to_string);
   END LOOP;
END;
/

An explanation of this code:

  • Declare an instance of the JSON_OBJECT_T type.
  • Declare an instance of the JSON_ELEMENT_T type.
  • For every row in the table, get the JSON document (info column) — which, remember is a CLOB, not a JSON document.
  • Create a JSON document from that CLOB by calling the JSON_OBJECT_T constructor function.
  • Call the GET member function of the JSON_OBJECT_T type on the l_species object to get the element for "name".
  • Call the TO_STRING member function of the JSON_ELEMENT_T type to convert the JSON element into a string.
  • Display that string.
And I see this output:
"Spider"
"Elephant"
"Shark"
"Ostrich"

(Well, I will see all that text, but maybe not in that exact order since I didn't specify an ORDER BY clause in my SELECT. Always good to remember that!)

That was the step-by-step approach, which you will want to use when you are just getting started with these types. If, however, all I wanted to do in PL/SQL was display the value (which I could, remember, do with "pure" SQL), I could write and execute nothing more than this:

BEGIN
   FOR rec IN (SELECT sp.info FROM json_species sp)
   LOOP
      dbms_output.put_line (
         json_object_t (rec.info).get ('name').to_string);
   END LOOP;
END;
/

Sure, nothing fancy there. And there's lots more to explore. So:

  1. If you reading this post before February 8, 2018, be sure to sign up for a CodeTalk session on JSON and PL/SQL I am doing with Darryl Hurley of Implestrat. He knows a lot more about this stuff than me! (For now, anyway!)
  2. Check out my LiveSQL script with the above code and more.
  3. I will be publishing more on this in the weeks to come.
JSON PL/SQL Database Relational database API Document

Published at DZone with permission of Steven Feuerstein, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Introduction to Couchbase for Oracle Developers and Experts: Part 2 - Database Objects
  • Designing a Blog Application Using Document Databases
  • Keep Calm and Column Wise
  • Migrating MuleSoft System API to AWS Lambda (Part 1)

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!