我需要从查询完全结构化JSON的结果。我可以在Postgres里,有一些内置的功能,可能是有用的看到。
作为一个例子,我使用的结构,如下所示:
-- Table: person
-- DROP TABLE person;
CREATE TABLE person
(
id integer NOT NULL,
name character varying(30),
CONSTRAINT person_pk PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE person
OWNER TO postgres;
-- Table: car
-- DROP TABLE car;
CREATE TABLE car
(
id integer NOT NULL,
type character varying(30),
personid integer,
CONSTRAINT car_pk PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE car
OWNER TO postgres;
-- Table: wheel
-- DROP TABLE wheel;
CREATE TABLE wheel
(
id integer NOT NULL,
whichone character varying(30),
serialnumber integer,
carid integer,
CONSTRAINT "Wheel_PK" PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE wheel
OWNER TO postgres;
和一些数据:
INSERT INTO person(id, name)
VALUES (1, 'Johny'),
(2, 'Freddy');
INSERT INTO car(id, type, personid)
VALUES (1, 'Toyota', 1),
(2, 'Fiat', 1),
(3, 'Opel', 2);
INSERT INTO wheel(id, whichone, serialnumber, carid)
VALUES (1, 'front', '11', 1),
(2, 'back', '12', 1),
(3, 'front', '21', 2),
(4, 'back', '22', 2),
(5, 'front', '3', 3);
正如我想有一个JSON对象,其中将包括人名单的结果,每个人都会有车的名单和车轮的每一辆汽车名单。
我想类似的东西,但它不是我想要的东西:
select json_build_object(
'Persons', json_build_object(
'person_name', person.name,
'cars', json_build_object(
'carid', car.id,
'type', car.type,
'comment', 'nice car', -- this is constant
'wheels', json_build_object(
'which', wheel.whichone,
'serial number', wheel.serialnumber
)
))
)
from
person
left join car on car.personid = person.id
left join wheel on wheel.carid = car.id
我想,我错过了一些GROUP BY和json_agg,但我不知道如何做到这一点。
我想有作为的结果是这样的:
{ "persons": [
{
"person_name": "Johny",
"cars": [
{
"carid": 1,
"type": "Toyota",
"comment": "nice car",
"wheels": [{
"which": "Front",
"serial number": 11
},
{
"which": "Back",
"serial number": 12
}]
},
{
"carid": 2,
"type": "Fiat",
"comment": "nice car",
"wheels": [{
"which": "Front",
"serial number": 21
},{
"which": "Back",
"serial number": 22
}]
}
]
},
{
"person_name": "Freddy",
"cars": [
{
"carid": 3,
"type": "Opel",
"comment": "nice car",
"wheels": [{
"which": "Front",
"serial number": 33
}]
}]
}]
}
http://www.jsoneditoronline.org/?id=7792a0a2bf11be724c29bb86c4b14577
你应该建立一个分层查询得到的分层结构的结果。
你想在一个JSON对象的许多人,所以使用json_agg()
聚集在一个JSON数组人。如此类推,一个人可以有多个汽车,你应该把属于一个人在一个JSON数组汽车。这同样适用于汽车和车轮。
select
json_build_object(
'persons', json_agg(
json_build_object(
'person_name', p.name,
'cars', cars
)
)
) persons
from person p
left join (
select
personid,
json_agg(
json_build_object(
'carid', c.id,
'type', c.type,
'comment', 'nice car', -- this is constant
'wheels', wheels
)
) cars
from
car c
left join (
select
carid,
json_agg(
json_build_object(
'which', w.whichone,
'serial number', w.serialnumber
)
) wheels
from wheel w
group by 1
) w on c.id = w.carid
group by personid
) c on p.id = c.personid;
该(格式化)结果:
{
"persons": [
{
"person_name": "Johny",
"cars": [
{
"carid": 1,
"type": "Toyota",
"comment": "nice car",
"wheels": [
{
"which": "front",
"serial number": 11
},
{
"which": "back",
"serial number": 12
}
]
},
{
"carid": 2,
"type": "Fiat",
"comment": "nice car",
"wheels": [
{
"which": "front",
"serial number": 21
},
{
"which": "back",
"serial number": 22
}
]
}
]
},
{
"person_name": "Freddy",
"cars": [
{
"carid": 3,
"type": "Opel",
"comment": "nice car",
"wheels": [
{
"which": "front",
"serial number": 3
}
]
}
]
}
]
}
如果你不熟悉嵌套派生表,你可以使用公共表表达式。这种变异说明了从朝最高级别的最嵌套的对象开始,该查询应建:
with wheels as (
select
carid,
json_agg(
json_build_object(
'which', w.whichone,
'serial number', w.serialnumber
)
) wheels
from wheel w
group by 1
),
cars as (
select
personid,
json_agg(
json_build_object(
'carid', c.id,
'type', c.type,
'comment', 'nice car', -- this is constant
'wheels', wheels
)
) cars
from car c
left join wheels w on c.id = w.carid
group by c.personid
)
select
json_build_object(
'persons', json_agg(
json_build_object(
'person_name', p.name,
'cars', cars
)
)
) persons
from person p
left join cars c on p.id = c.personid;
我想出了这个解决方案。这是相当紧凑,在任何给定的情况下工作。然而,不知道影响是什么性能上的比较其他的解决方案,让更多的使用json_build_object的时候。使用row_to_json超过json_build_object的优点是,所有的工作都罩,这让查询更易读下进行。
SELECT json_build_object('persons', json_agg(p)) persons
FROM (
SELECT
person.name person_name,
(
SELECT json_agg(row_to_json(c))
FROM (
SELECT
id carid,
type,
(
SELECT json_agg(row_to_json(w))
FROM (
SELECT
whichone which,
serialnumber
FROM wheel
WHERE wheel.carid = car.id
) w
) wheels
FROM car
WHERE car.personid = person.id
) c
) AS cars
FROM person
) p