1

I have a query, the output is like this:

    1  2 3 4 5 6 7 8 9 10 11 12 13
    -  - - - - - - - - -   -  - -    
    40 20 22 10 0 0 0 0 0 0 0 0 0

I want to convert the output to one column and the column is like below:

    output
    -----------
 {"1":40,"2":20,"3":22,"4":10,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0,"13":0}
0

2 Answers 2

7

You can use the JSON formatting 'trick' in SQL Developer.

enter image description here

Full scenario:

CREATE TABLE JSON_SO (
    "1"   INTEGER,
    "2"   INTEGER,
    "3"   INTEGER,
    "4"   INTEGER,
    "5"   INTEGER,
    "6"   INTEGER
);

INSERT INTO JSON_SO VALUES (
    40,
    20,
    22,
    10,
    0,
    0
);

select /*json*/ * from json_so;

And the output when executing with F5 (Execute as Script):

{
   "results":[
      {
         "columns":[
            {
               "name":"1",
               "type":"NUMBER"
            },
            {
               "name":"2",
               "type":"NUMBER"
            },
            {
               "name":"3",
               "type":"NUMBER"
            },
            {
               "name":"4",
               "type":"NUMBER"
            },
            {
               "name":"5",
               "type":"NUMBER"
            },
            {
               "name":"6",
               "type":"NUMBER"
            }
         ],
         "items":[
            {
               "1":40,
               "2":20,
               "3":22,
               "4":10,
               "5":0,
               "6":0
            }
         ]
      }
   ]
}

Note that the JSON output happens client-side via SQL Developer (this also works in SQLcl) and I formatted the JSON output for display here using https://jsonformatter.curiousconcept.com/

This will work with any version of Oracle Database that SQL Developer supports, while the JSON_OBJECT() function was introduced in Oracle Database 12cR2 - if you want to have the DB format the result set to JSON for you.

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

1 Comment

you can also use SET SQLFORMAT json...or csv, insert, html, delimited, loader, xml
3

If you want Oracle DB server to return the result as JSON, you can do query as below -

 SELECT JSON_OBJECT ('1' VALUE col1, '2' VALUE col2, '3' VALUE col3) FROM table

6 Comments

which SqlDeveloper version are you using?
Version 18.2.0.183
Can you try this way? SELECT JSON_OBJECT ('1' VALUE col1, '2' VALUE col2, '3' VALUE col3) FROM table;
using the json comment, with f5, the resultset will be formatted as json client side when executed in SQL Developer - if you use json_object() the db will do the json-ification work
@shakhawat : SELECT JSON_OBJECT ('1' VALUE col1, '2' VALUE col2, '3' VALUE col3) FROM table; It works , thank you please edit your answer and I accept your question as a correct answer
|

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.