Return JSON 在 Postgres 中
Return JSON in Postgres
我在 postgres 中创建了一个 returns json.
的函数
该函数在 API 和 return 中使用:
[
{
"jobfeed_beroepen_list": [
{
"profession_code": 3674,
"profession_descr": "Manusje van alles (boodschappen, post en goederen)"
},
{
"profession_code": 4107,
"profession_descr": "Algemeen medewerker"
}
]
}
]
但我想要它 return 这个:
[
{
"profession_code": 3674,
"profession_descr": "Manusje van alles (boodschappen, post en goederen)"
},
{
"profession_code": 4107,
"profession_descr": "Algemeen medewerker"
}
]
问题:我必须在代码中更改什么?
----------------这是函数-------------
CREATE OR REPLACE FUNCTION api.fnc_jobfeed_beroepen(
)
RETURNS TABLE(jobfeed_beroepen_list jsonb)
LANGUAGE 'plpgsql'
COST 100
VOLATILE
ROWS 1000
AS $BODY$
DECLARE
BEGIN
jobfeed_beroepen_list := to_jsonb(array_agg(t)) FROM
(SELECT profession_code, profession_descr
FROM skillcv.jobfeed_beroepen
WHERE use = 1
) AS t;
RETURN QUERY SELECT jobfeed_beroepen_list;
END;
$BODY$;
您可以使用 json 聚合。那将是:
return query
select jsonb_agg(
jsonb_build_object(
'profession_code', profession_code,
'profession_descr', profession_descr
)
) as res
from skillcv.jobfeed_beroepen
where use = 1
CREATE OR REPLACE FUNCTION api.fnc_jobfeed_beroepen()
RETURNS TABLE(jobfeed_beroepen_list jsonb)
LANGUAGE 'plpgsql'
COST 100
VOLATILE
ROWS 1000
AS $BODY$
DECLARE
begin
return query(select jsonb_agg(
jsonb_build_object(
'profession_code', profession_code,
'profession_descr', profession_descr
)
) as res
from skillcv.jobfeed_beroepen
where use = 1)
end;
$BODY$;
我在 postgres 中创建了一个 returns json.
的函数该函数在 API 和 return 中使用:
[
{
"jobfeed_beroepen_list": [
{
"profession_code": 3674,
"profession_descr": "Manusje van alles (boodschappen, post en goederen)"
},
{
"profession_code": 4107,
"profession_descr": "Algemeen medewerker"
}
]
}
]
但我想要它 return 这个:
[
{
"profession_code": 3674,
"profession_descr": "Manusje van alles (boodschappen, post en goederen)"
},
{
"profession_code": 4107,
"profession_descr": "Algemeen medewerker"
}
]
问题:我必须在代码中更改什么?
----------------这是函数-------------
CREATE OR REPLACE FUNCTION api.fnc_jobfeed_beroepen(
)
RETURNS TABLE(jobfeed_beroepen_list jsonb)
LANGUAGE 'plpgsql'
COST 100
VOLATILE
ROWS 1000
AS $BODY$
DECLARE
BEGIN
jobfeed_beroepen_list := to_jsonb(array_agg(t)) FROM
(SELECT profession_code, profession_descr
FROM skillcv.jobfeed_beroepen
WHERE use = 1
) AS t;
RETURN QUERY SELECT jobfeed_beroepen_list;
END;
$BODY$;
您可以使用 json 聚合。那将是:
return query
select jsonb_agg(
jsonb_build_object(
'profession_code', profession_code,
'profession_descr', profession_descr
)
) as res
from skillcv.jobfeed_beroepen
where use = 1
CREATE OR REPLACE FUNCTION api.fnc_jobfeed_beroepen()
RETURNS TABLE(jobfeed_beroepen_list jsonb)
LANGUAGE 'plpgsql'
COST 100
VOLATILE
ROWS 1000
AS $BODY$
DECLARE
begin
return query(select jsonb_agg(
jsonb_build_object(
'profession_code', profession_code,
'profession_descr', profession_descr
)
) as res
from skillcv.jobfeed_beroepen
where use = 1)
end;
$BODY$;