【问题标题】:How to merge two json object into an array in postgres如何将两个json对象合并到postgres中的数组中
【发布时间】:2021-03-03 09:35:20
【问题描述】:

我有两个 jsonb 值。我想合并它,但是我坚持将它合并在一起。这是我的代码

CREATE OR REPLACE FUNCTION emr.azintin(p_msg text)
 RETURNS character varying
 LANGUAGE plpgsql
AS $function$
DECLARE
    v_msgar     text[];
    v_msgln     text;
    v_currusr   afm_usr.user_id%type := 'SYSEXCH';
    v_currdt    afm_usr.insert_date%type := CURRENT_TIMESTAMP;
    macres      azt_macres%rowtype;
    orddet      lbt_orddet%rowtype;
    v_resultid  character varying ;
    v_reading   character varying ;

BEGIN   
         v_msgar := array['OBR||||^^^3972^2102001420||||20210227133222|25||||3
                            OBX|||0^RBC||4.99|^10â¶/μL|3.85-5.78||||F
                            OBX|||1^HGB||14.4|^g/dL|12.0-17.2||||F
                            OBX|||2^MCV||84.5|^fL|78.0-96.0||||F
                            OBX|||3^HCT||42.2|^%|34.8-50.9||||F
                            OBX|||4^MCH||28.90|^pg|26.40-33.20||||F
                            OBX|||5^MCHC||34.2|^g/dL|31.8-36.7|||<EOT>|F
                            OBX|||6^RDWsd||39.2|^fL|0.0-0.0||||F'];
              
            SELECT json_agg(jsonb_build_object('resultValue',split_part(parts[6],'^',1)))
               INTO v_reading
               FROM unnest(v_msgar) as elem,
                     regexp_split_to_array(elem, '\|') as parts
               WHERE parts[1] = 'OBX';
              
            SELECT json_agg(jsonb_build_object('resultId',split_part(parts[4],'^',2)))
               INTO v_resultid
               FROM unnest(v_msgar) as elem,
                     regexp_split_to_array(elem, '\|') as parts
               WHERE parts[1] = 'OBX';
              
            select concat(v_resultid || v_reading) 
               INTO macres.test_result
               FROM unnest(v_msgar) as elem,
                     regexp_split_to_array(elem, '\|') as parts
               WHERE parts[1] = 'OBX';
        
            SELECT split_part(items[5], '^', 5)
                  INTO macres.specimen_id
                FROM (
                  SELECT string_to_array(element, '|') as items
                 FROM unnest(v_msgar) as t(element)) t   
            WHERE items[1] = 'OBR';
    

    END IF;
END
$function$
;

我设法得到看起来像这样的 v_reading 和 v_resultid

 [{"resultValue": "5.20"}, {"resultValue": "14.2"}, {"resultValue": "81.7"}, {"resultValue": "42.5"}, {"resultValue": "27.20"}, {"resultValue": "33.3"}, {"resultValue": "40.6"}, {"resultValue": "15.9"}, {"resultValue": "188"}, {"resultValue": "8.1"}, {"resultValue": "0.15"}, {"resultValue": "9.4"}, {"resultValue": "46.2"}, {"resultValue": "4.6"}, {"resultValue": "1.1"}, {"resultValue": "23.6"}, {"resultValue": "0.4"}, {"resultValue": "8.7"}, {"resultValue": "3.1"}, {"resultValue": "67.7"}, {"resultValue": "56"}, {"resultValue": "105"}, {"resultValue": ""}] 
[{"resultId": "RBC"}, {"resultId": "HGB"}, {"resultId": "MCV"}, {"resultId": "HCT"}, {"resultId": "MCH"}, {"resultId": "MCHC"}, {"resultId": "RDWsd"}, {"resultId": "RDWcv"}, {"resultId": "PLT"}, {"resultId": "MPV"}, {"resultId": "PCT"}, {"resultId": "PDWsd"}, {"resultId": "PDWcv"}, {"resultId": "WBC"}, {"resultId": "LYM"}, {"resultId": "LYMP"}, {"resultId": "MID"}, {"resultId": "MIDP"}, {"resultId": "GRA"}, {"resultId": "GRAP"}, {"resultId": "PLCR"}, {"resultId": "PLCC"}, {"resultId": "RBCHistogram"}]

如何将这两个 json 组合成这样的样子

[{"resultid": "RBC", "resultValue": "5.20"}, {"resultid": "HGB", "resultValue": "5.20"}, {"resultid": "MCV", "resultValue": "5.20"}, {"resultid": "HCT", "resultValue": "5.20"}, {"resultid": "MCH", "resultValue": "5.20"}, {"resultid": "MCHC", "resultValue": "5.20"}, {"resultid": "RDWsd", "resultValue": "5.20"}, {"resultid": "RDWcv", "resultValue": "5.20"}, {"resultid": "PLT", "resultValue": "5.20"}, {"resultid": "MPV", "resultValue": "5.20"}, {"resultid": "PCT", "resultValue": "5.20"}, {"resultid": "PDWsd", "resultValue": "5.20"}, {"resultid": "PDWcv", "resultValue": "5.20"}, {"resultid": "WBC", "resultValue": "5.20"}, {"resultid": "LYM", "resultValue": "5.20"}, {"resultid": "LYMP", "resultValue": "5.20"}, {"resultid": "MID", "resultValue": "5.20"}, {"resultid": "MIDP", "resultValue": "5.20"}, {"resultid": "GRA", "resultValue": "5.20"}, {"resultid": "GRAP", "resultValue": "5.20"}, {"resultid": "PLCR", "resultValue": "5.20"}, {"resultid": "PLCC", "resultValue": "5.20"}, {"resultid": "RBCHistogram", "resultValue": "5.20"}]

我可以简单地做这些

            SELECT json_agg(jsonb_build_object('resultid',split_part(parts[4],'^',2), 'resultValue',split_part(parts[6],'^',1)))
               INTO macres.test_result
               FROM unnest(v_msgar) as elem,
                     regexp_split_to_array(elem, '\|') as parts
               WHERE parts[1] = 'OBX';

但问题是我需要在 split_part(parts[4],'^',2) 中添加一个条件,这就是为什么我将它分成 v_reading 和 v_resultid。

我要设置条件

IF v_resultid = 'MCHC' THEN
   v_reading = null
END ID

但不知何故,如果数组找到 MCHC,它仍然会读取读数。我想从我的 json 中删除 MCHC

【问题讨论】:

  • 不清楚你在这里做什么。 JSON 值不适合您的数组,该数组无效......所以,这里有两个选项:您是否希望我们编写一个完整的查询(我已经在您之前的问题上做过几次) - 我会这样做在一个查询中,而不是在单独的......或者您只是想知道如何合并两个 JSON 对象。那么请删除整个令人困惑的代码以尽量减少您的问题。

标签: json postgresql stored-procedures merge jsonb


【解决方案1】:

加入两个 JSON 对象:

demo:db<>fiddle

SELECT
    jsonb_agg(a.element || b.element)                        -- 4
FROM (
    SELECT * FROM jsonb_array_elements(                      -- 1
        '[{"resultValue": "5.20"}, {"resultValue": "14.2"}, {"resultValue": "81.7"}, {"resultValue": "42.5"}, {"resultValue": "27.20"}, {"resultValue": "33.3"}, {"resultValue": "40.6"}, {"resultValue": "15.9"}, {"resultValue": "188"}, {"resultValue": "8.1"}, {"resultValue": "0.15"}, {"resultValue": "9.4"}, {"resultValue": "46.2"}, {"resultValue": "4.6"}, {"resultValue": "1.1"}, {"resultValue": "23.6"}, {"resultValue": "0.4"}, {"resultValue": "8.7"}, {"resultValue": "3.1"}, {"resultValue": "67.7"}, {"resultValue": "56"}, {"resultValue": "105"}, {"resultValue": ""}]'::jsonb
    ) WITH ORDINALITY as a(element, index)                   -- 2
) a
JOIN (
    SELECT * FROM jsonb_array_elements(
        '[{"resultId": "RBC"}, {"resultId": "HGB"}, {"resultId": "MCV"}, {"resultId": "HCT"}, {"resultId": "MCH"}, {"resultId": "MCHC"}, {"resultId": "RDWsd"}, {"resultId": "RDWcv"}, {"resultId": "PLT"}, {"resultId": "MPV"}, {"resultId": "PCT"}, {"resultId": "PDWsd"}, {"resultId": "PDWcv"}, {"resultId": "WBC"}, {"resultId": "LYM"}, {"resultId": "LYMP"}, {"resultId": "MID"}, {"resultId": "MIDP"}, {"resultId": "GRA"}, {"resultId": "GRAP"}, {"resultId": "PLCR"}, {"resultId": "PLCC"}, {"resultId": "RBCHistogram"}]'::jsonb
    ) WITH ORDINALITY as b(element, index)
) b
ON a.index = b.index                                         -- 3
  1. 将 JSON 数组提取到每个元素一行。
  2. WITH ORDINALITY 为每个展开的记录添加一个行数,以标识数组中的原始位置
  3. 在索引中加入两个提取的 JSON 元素集
  4. 使用|| 运算符合并两个JSON 列(仅适用于jsonb noth 类型和json 类型,但仍应使用)。最后,您可以将这些合并的对象聚合到一个新的 JSON 数组中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-17
    • 1970-01-01
    相关资源
    最近更新 更多