如何从多个值中对象row_to_json和array_agg [英] How to object in row_to_json and array_agg from multiple value
问题描述
我设法创建一个jsonb输出,但是我需要将其作为对象
I manage to create a jsonb output, however I need to make it as object
我当前的代码
create function extract_data()
returns jsonb
as
$$
declare
v_msgar text[];
v_result jsonb;
analyzer_test_full_desc character varying;
data_reading character varying;
begin
v_msgar := array['P|61||||^^||^^|U||||||||||||||||||||||||||<CR>',
'O|61|15^1^15|KK4002259|GLU^Glucose (GOD-POD Method)^^',
'R|170|AST^Aspartate Aminotransferase^^F|22.657989^^^^',
'R|171|ALP^Alkaline phosphatase^^F|107.636995^^^^',
'R|172|TP^Total Protein^^F|85.245151^^^^',
'R|173|TG^Triglycerides^^F|1.348633^^^^',
'R|174|HDL^HDL-Cholesterol^^F|1.238458^^^^',
'R|175|CHOL^Total Cholesterol^^F|5.073630^^^^',
'R|176|UA^Uric Acid^^F|309.705876^^^^',
'R|177|BUN^Urea^^F|4.412234^^^^'];
SELECT split_part(items[3], '^', 1)
INTO analyzer_test_full_desc
FROM (
SELECT string_to_array(element, '|') as items
FROM unnest(v_msgar) as t(element)) t
WHERE items[1] = 'R';
SELECT split_part(items[4], '^', 1)
INTO data_reading
FROM (
SELECT string_to_array(element, '|') as items
FROM unnest(v_msgar) as t(element)) t
WHERE items[1] = 'R';
SELECT array_to_json(array_agg(row_to_json(t)))
FROM (
SELECT analyzer_test_full_desc as resultId, data_reading as resultValue
) t INTO v_result;
return v_result;
end;
$$
language plpgsql;
输出为
[{"resultid":"AST","resultvalue":"22.657989"}]
我希望输出结果是这样的
I expected the out put is something like this
[{"resultid":"AST","resultvalue":"22.657989"},{"resultid":"ALP","resultvalue":"107.636995";},{"resultid":"TP","resultvalue":"85.245151"]
我尝试循环执行select语句,但是仍然没有解决方法.
I try to loop the select statement however still didnt have the solution.
推荐答案
您不需要pl/pgsql即可执行此操作.在一个(内部)查询中提取所需的数据部分,然后像这样聚合它们:
You do not need pl/pgsql to do this. Extract the data parts that you need in one (inner) query and then aggregate them like this:
create or replace function extract_data(text[]) returns jsonb as
$$
select jsonb_agg(to_jsonb(t.*)) from
(
select split_part(split_part(arr, '|', 3), '^', 1) as "resultId",
split_part(split_part(arr, '|', 4), '^', 1) as "resultValue"
from unnest($1) arr
where split_part(arr, '|', 1) = 'R'
) t;
$$ language sql;
select extract_data(array[
'P|61||||^^||^^|U||||||||||||||||||||||||||<CR>',
'O|61|15^1^15|KK4002259|GLU^Glucose (GOD-POD Method)^^',
'R|170|AST^Aspartate Aminotransferase^^F|22.657989^^^^',
'R|171|ALP^Alkaline phosphatase^^F|107.636995^^^^',
'R|172|TP^Total Protein^^F|85.245151^^^^',
'R|173|TG^Triglycerides^^F|1.348633^^^^',
'R|174|HDL^HDL-Cholesterol^^F|1.238458^^^^',
'R|175|CHOL^Total Cholesterol^^F|5.073630^^^^',
'R|176|UA^Uric Acid^^F|309.705876^^^^',
'R|177|BUN^Urea^^F|4.412234^^^^'
]);
为了使函数可重用,请将数据数组作为参数传递,而不是在函数体内对其进行硬编码.
In order to make the function reusable pass the data array as an argument rather than hard-code it inside the function body.
这篇关于如何从多个值中对象row_to_json和array_agg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!