简体   繁体   English

Postgres如何迭代嵌套的JSON object

[英]Postgres how to iterate over nested JSON object

I have an object as below.我有一个 object 如下。 I would like to iterate over each user in insert and insert them into my database.我想在插入中遍历每个用户并将它们插入到我的数据库中。 I'm stuck at how to reach the inner objects.我被困在如何到达内部物体上。 I have a code like below and I'm stuck on how to go further into the obejct, it only gets insert , delete and update .我有一个像下面这样的代码,我被困在如何将 go 进一步放入对象中,它只会得到insertdeleteupdate I am using postgresql for this.我为此使用 postgresql。

I see ->> int Get JSON array element as text '[1,2,3]'::json->>2 in the docs but not sure how to incorporate into my code我在文档中看到->> int Get JSON array element as text '[1,2,3]'::json->>2但不确定如何合并到我的代码中

DECLARE
   _key   text;
   _value text;
BEGIN
    FOR _key, _value IN
       SELECT * FROM jsonb_each_text($1)
    LOOP
       RAISE NOTICE '%: %', _key, _value;
    END LOOP;

    RETURN input;
END




 {
      insert: {
        jsmith:
        { 
          name: 'John Smith',
          mail: 'JSmith@smith.com',
          jobTitle: 'Lead',
          hasImage: true,
          teamId: '35' 
        },
        lmark:
        {
          name: 'Laurendy Mark',
          mail: 'LMark@mark.com',
          jobTitle: 'System Admin',
          hasImage: true,
          teamId: '40'
        }
      },
      delete: {
        lbeth
        {
          name: 'Lisa Beth',
          mail: 'LBeth@smith.com',
          jobTitle: 'Assistant Director',
          hasImage: true,
          teamId: '40',
          uid: '200'
        }
      },
      update: {}
    }

You don't really need a PL/pgSQL function for this.为此,您实际上并不需要 PL/pgSQL function。 This can be done using a data modifying CTE这可以使用数据修改 CTE来完成

with input (col) as (
  values ('
    {
       "insert":{ 
        "jsmith":{"name":"John Smith", "mail":"JSmith@smith.com","jobTitle":"Lead","hasImage":true,"teamId":35},
        "lmark":{"name":"Laurendy Mark","mail":"LMark@mark.com","jobTitle":"System Admin","hasImage":true,"teamId":40}
       },
       "delete":{
         "lbeth":{"name":"Lisa Beth","mail":"LBeth@smith.com","jobTitle":"Assistant Director","hasImage":true,"teamId":40,"uid":200}
       },
       "update":{}
    }'::jsonb)
), do_insert as (
  insert into the_table (name, mail, job_title, has_image, team_id)
  select i.value ->> 'name', 
         i.value ->> 'mail',
         i.value ->> 'jobTitle',
         (i.value ->> 'hasImage')::boolean,
         (i.value ->> 'teamId')::int
  from input
   cross join jsonb_each(col -> 'insert') i
  returning *
), do_delete as (
  delete from the_table
  where uid in (select (i.value ->> 'uid')::int
                 from input
                   cross join jsonb_each(col -> 'delete') i)
)                   
update the_table
  set has_image = (i.value ->> 'hasImage')::boolean
from input
 cross join jsonb_each(col -> 'update') i
where (i.value ->> 'uid')::int = the_table.uid
;

Online example 在线示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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