Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructing JSON string from Oracle DB

Tags:

json

oracle

I have a Web application which gets it data from a JSON string. The JSON is in the following format

{
    "contacts": [{
        "type": "contact",
        "name": "John Doe",
        "contact": 1,
        "links": ["Spouse", "Friends","Jane Doe","Harry Smith"]
                 }]
}

Now this is a sample data. My actual DB is in Oracle. My question would be how do I construct this JSON from Oracle.

like image 533
AnaMaria Avatar asked Jul 09 '13 06:07

AnaMaria


1 Answers

This is the best method I've come across: http://ora-00001.blogspot.sk/2010/02/ref-cursor-to-json.html.

To summarise:

  • Use the DBMS_XMLGEN package to generate XML from a SYS_REFCURSOR.
  • Then transform it using this XSLT.

I like it because there's no manual generation and because you have the option of returning XML too by skipping the final transformation.

like image 194
manadart Avatar answered Oct 01 '22 10:10

manadart