1

I am trying to get JSON string using Oracle

My Oracle version is 12.1.0.2.0 and as I can't upgrade, I can't use JSON_ARRAYAGG.

The Json I want is exactly same with the picture

2

2 Answers 2

3

A Client Based Solution, where client is Oracle SQL Developer.

Query:

SELECT /*json*/ ID, NAME CUST_NAME, ADDRESS,
 CURSOR(
  SELECT ACCOUNT_ID,
         NAME ACCOUNT_NAME,
         BALANCE
  FROM ACCOUNTS
  WHERE CUST.ID = CUSTOMER_ID) AS "accounts",
  '../customers/' || ID || '/pic' AS "$signature"
FROM customers cust

The interesting part of this is the /*json*/ bit. That tells our script engine to take the results and format to JSON. It also supports csv, insert statements, xml, html, etc.

The query returns 3 rows from my table, and an accounts bit, which is some nested results per row via the CURSOR bit of the query.

When executed via F5 (execute as script), SQL Developer returns this JSON

{  
   "results":[  
      {  
         "columns":[  
            {  
               "name":"ID",
               "type":"NUMBER"
            },
            {  
               "name":"CUST_NAME",
               "type":"VARCHAR2"
            },
            {  
               "name":"ADDRESS",
               "type":"VARCHAR2"
            },
            {  
               "name":"accounts",
               "type":"REFCURSOR"
            },
            {  
               "name":"$signature",
               "type":"VARCHAR2"
            }
         ],
         "items":[  
            {  
               "id":1,
               "cust_name":"Jeff",
               "address":"101 Maple Ln",
               "accounts":[  
                  {  
                     "account_id":100,
                     "account_name":"College Fund",
                     "balance":25.99
                  },
                  {  
                     "account_id":101,
                     "account_name":"NewCar",
                     "balance":30000
                  }
               ],
               "$signature":"..\/customers\/1\/pic"
            },
            {  
               "id":2,
               "cust_name":"Kris",
               "address":"911 Pine Dr",
               "accounts":[  
                  {  
                     "account_id":200,
                     "account_name":"Checking",
                     "balance":42.25
                  },
                  {  
                     "account_id":201,
                     "account_name":"Savings",
                     "balance":64000
                  }
               ],
               "$signature":"..\/customers\/2\/pic"
            },
            {  
               "id":3,
               "cust_name":"Colm",
               "address":"404 Irish Corner",
               "accounts":[  
                  {  
                     "account_id":300,
                     "account_name":"Potatoes",
                     "balance":2500.75
                  },
                  {  
                     "account_id":301,
                     "account_name":"Speeding Tickets",
                     "balance":1900
                  }
               ],
               "$signature":"..\/customers\/3\/pic"
            }
         ]
      }
   ]
}

We also have a mid-tier solution, Oracle REST Data Services. This allows you to create a RESTful Service with a GET handler to run SQL or a PL/SQL block, where the results are returned in JSON.

Sign up to request clarification or add additional context in comments.

8 Comments

thanks for the assist @alex-poole - the /* always gets me
No worries, me too - usually when writing SQL*Plus or SQL*Loader, though I've now started escaping that * offline which confuses people... This is nice BTW. I'm not sure the formatting options (this version and set sqlformat), or the SQLcl equivalents, are widely known or appreciated so it's always good when you have a chance to plug them *8-)
ps @AlexPoole we just open sourced the code that does that formatting bits, it's java and on github if you're curious github.com/oracle/dbtools-commons/tree/master/common
Ooh, nice. The parsing stuff looks interesting too, there have been questions about how to parse scripts into statement. Thanks!
@FatihKara I would only say or add, SQLDev is free, it comes WITH the database, and there's no 'installer' - you just unzip it.
|
0

JSON support in Oracle is pretty limited before 12.2. This blog has posts on 4 different ways to generate JSON in Oracle 12.1.

The two that are relatively easy to do in plain PL/SQL are:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.