简体   繁体   中英

How can I easily convert an outer joined SQL table to a json object?

I'm using nodejs and oracledb, and I am able to turn a simple query into a json file.

What I would like to do is turn the result of my oracle query into a json object. The problem becomes that my joins

For example, lets say I have a table of users, with names and id's. I also have a table of network logins, each one containing a user's id. What I would like to do is create a json for each user, with values for their names, id's, and a list of their network logins.

With the outer join, I get all the relevant data, but each id/name is repeated, so I get several json entries for each user.

I don't have your tables nor data, so - here's an example based on Scott's sample schema and its departments and employees table:

SQL> select d.dname, d.loc, e.ename, e.job, e.sal
  2  from dept d join emp e on e.deptno = d.deptno
  3  order by d.dname;

DNAME          LOC           ENAME      JOB              SAL
-------------- ------------- ---------- --------- ----------
ACCOUNTING     NEW YORK      MILLER     CLERK         1300.1
ACCOUNTING     NEW YORK      KING       PRESIDENT       5000
ACCOUNTING     NEW YORK      CLARK      MANAGER         2450
RESEARCH       DALLAS        ADAMS      CLERK         1100.1
RESEARCH       DALLAS        FORD       ANALYST         3000
RESEARCH       DALLAS        JONES      MANAGER         2975
RESEARCH       DALLAS        SMITH      CLERK          800.1
RESEARCH       DALLAS        SCOTT      ANALYST         3000
SALES          CHICAGO       WARD       SALESMAN      1250.1
SALES          CHICAGO       TURNER     SALESMAN      1500.1
SALES          CHICAGO       ALLEN      SALESMAN      1600.1
SALES          CHICAGO       JAMES      CLERK          950.1
SALES          CHICAGO       BLAKE      MANAGER         2850
SALES          CHICAGO       MARTIN     SALESMAN      1250.1

14 rows selected.

SQL>

If I understood you correctly, you want to get departments as "master" data and employees that work in that department as its "details". If that's so, use json_arrayagg :

SQL> select
  2    json_object ('OBJ' value json_object
  3                    ('DEPARTMENT' value json_object
  4                        ('NAME' value d.dname,
  5                         'LOCATION'   value d.loc
  6                        )
  7                    ),
  8                  'EMPS' value json_arrayagg
  9                    (json_object ('NAME'   value e.ename,
 10                                  'JOB'    value e.job,
 11                                  'SALARY' value e.sal
 12                                 )
 13                    )
 14                ) obj
 15  from dept d join emp e on e.deptno = d.deptno
 16  group by d.dname, d.loc;

Result:

OBJ
--------------------------------------------------------------------------------
{"OBJ":{"DEPARTMENT":{"NAME":"SALES","LOCATION":"CHICAGO"}},"EMPS":[{"NAME":"WAR
D","JOB":"SALESMAN","SALARY":1250.1},{"NAME":"MARTIN","JOB":"SALESMAN","SALARY":
1250.1},{"NAME":"BLAKE","JOB":"MANAGER","SALARY":2850},{"NAME":"JAMES","JOB":"CL
ERK","SALARY":950.1},{"NAME":"ALLEN","JOB":"SALESMAN","SALARY":1600.1},{"NAME":"
TURNER","JOB":"SALESMAN","SALARY":1500.1}]}

{"OBJ":{"DEPARTMENT":{"NAME":"RESEARCH","LOCATION":"DALLAS"}},"EMPS":[{"NAME":"J
ONES","JOB":"MANAGER","SALARY":2975},{"NAME":"SCOTT","JOB":"ANALYST","SALARY":30
00},{"NAME":"SMITH","JOB":"CLERK","SALARY":800.1},{"NAME":"ADAMS","JOB":"CLERK",
"SALARY":1100.1},{"NAME":"FORD","JOB":"ANALYST","SALARY":3000}]}

{"OBJ":{"DEPARTMENT":{"NAME":"ACCOUNTING","LOCATION":"NEW YORK"}},"EMPS":[{"NAME
":"CLARK","JOB":"MANAGER","SALARY":2450},{"NAME":"MILLER","JOB":"CLERK","SALARY"
:1300.1},{"NAME":"KING","JOB":"PRESIDENT","SALARY":5000}]}


SQL>

If you take any of these and copy/paste it into eg JSON formatter and validator to check how it really looks like and if it is valid, you'd get

{
   "OBJ":{
      "DEPARTMENT":{
         "NAME":"SALES",
         "LOCATION":"CHICAGO"
      }
   },
   "EMPS":[
      {
         "NAME":"WARD",
         "JOB":"SALESMAN",
         "SALARY":1250.1
      },
      {
         "NAME":"MARTIN",
         "JOB":"SALESMAN",
         "SALARY":1250.1
      },
      {
         "NAME":"BLAKE",
         "JOB":"MANAGER",
         "SALARY":2850
      },
      {
         "NAME":"JAMES",
         "JOB":"CLERK",
         "SALARY":950.1
      },
      {
         "NAME":"ALLEN",
         "JOB":"SALESMAN",
         "SALARY":1600.1
      },
      {
         "NAME":"TURNER",
         "JOB":"SALESMAN",
         "SALARY":1500.1
      }
   ]
}

which is a valid JSON.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM