Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to combine/concat two or more rows in a single json object in SQL?

Kinjan BhavsarJun 15 2020 — edited Jun 15 2020

Hi All,

I want to combine multiple rows in a single JSON_OBJECT but I am not able to find a direct way, is it possible to achieve it using SQL?

Here is my test script

create table test_tbl(id number, name varchar2(1000));

insert into test_tbl values(1, 'Test1');

insert into test_tbl values(2,'Test2');

select json_object('Data' VALUE json_arrayagg(

                                    json_object(name value to_char(id))

                                  ))

from test_tbl;

The JSON output received as a result is

{

     "Data": [{

               "Test1": "1"

              },

              {

               "Test2": "2"

              }

             ]

}

And the JSON which I need is the following

{

"Data": {

          "Test1": "1",

          "Test2": "2"

        }

}

Can someone suggest whether this is possible to achieve using JSON functions of SQL?

This post has been answered by cormaco on Jun 15 2020
Jump to Answer
Comments
Post Details
Added on Jun 15 2020
4 comments
8,984 views