Home » Software Development » Use the Power of your Database: XML and JSON

About Simon Martinelli

Simon Martinelli
Simon Martinelli is a passionate Java, performance optimization and application integration expert and an active member of the Java community process (JSR-352 Java Batch and JSR-354 Money and Currency API). He is the owner of 72 Services LLC and an adjunct professor at Berne University of Applied Science in Switzerland, teaching software architecture and design and persistence technologies

Use the Power of your Database: XML and JSON

Today databases have a lot of functionality that is often not used by software developers because they simply not know that this exists.
But knowing about this features can save a lot of time because you may write less code.

One of this hidden gems is the ability of producing XML or JSON data using a SQL SELECT statement.
In the examples Oracle Database is used but these features are also available in other Databases like PostgreSQL or SQL Server.

Model

Let’s assume we have the following model and we want to have employees with their phones.

XML

In Oracle Database producing XML would look like this.

1
2
3
4
5
6
7
8
select xmlelement(name "employees",
                  xmlagg(xmlelement(name "employee",
                                    xmlattributes(EMPLOYEE.ID, EMPLOYEE.NAME),
                                    xmlelement(name "phones", (select xmlagg(xmlelement(name "phone",
                                                                             xmlattributes(PHONE.PHONENUMBER, PHONE.TYPE)))
                                                               from PHONE
                                                               where PHONE.EMPLOYEE_ID = EMPLOYEE.ID)))))
from "EMPLOYEE"

The result of the query will be this XML:

01
02
03
04
05
06
07
08
09
10
11
12
13
<employees>
    <employee ID="1" NAME="Ursula Friedman">
        <phones>
            <phone PHONENUMBER="031 333 11 12" TYPE="WORK"/>
        </phones>
    </employee>
    <employee ID="2" NAME="Hans Boss">
        <phones>
            <phone PHONENUMBER="031 333 11 01" TYPE="HOME"/>
            <phone PHONENUMBER="032 311 43 12" TYPE="WORK"/>
        </phones>
    </employee>
</employees>

JSON

For sure this also works for JSON in Oracle Database.

01
02
03
04
05
06
07
08
09
10
SELECT
    json_arrayagg("employee".employee)
FROM (SELECT
        json_object(
            KEY 'id' value EMPLOYEE.ID,
            KEY 'name' value EMPLOYEE.NAME,
            KEY 'phones' value json_arrayagg(json_object(KEY 'number' value PHONE.PHONENUMBER, KEY 'type' value PHONE.TYPE))
        ) employee
    FROM EMPLOYEE JOIN PHONE ON PHONE.EMPLOYEE_ID = EMPLOYEE.ID
    GROUP BY EMPLOYEE.ID, EMPLOYEE.NAME) "employee"

The query produces this result:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[
  {
    "id": 1,
    "name": "Ursula Friedman",
    "phones": [
      {
        "number": "031 333 11 12",
        "type": "WORK"
      }
    ]
  },
  {
    "id": 2,
    "name": "Hans Boss",
    "phones": [
      {
        "number": "031 333 11 01",
        "type": "HOME"
      },
      {
        "number": "032 311 43 12",
        "type": "WORK"
      }
    ]
  }
]

Conclusion

Knowing the features of your database can save you time. Start reading the manual of your database today. Have fun!

Published on Java Code Geeks with permission by Simon Martinelli, partner at our JCG program. See the original article here: Use the Power of your Database: XML and JSON

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments