将 json 插入数据库

Insert json to db

我正在尝试将 json 数据插入数据库。示例 json:

{ 
"events": 
  [{                                                                                                   
     "timestamp": 1298734,                                                                                               
     "message": "START RequestId: bjn937645",                                                    
     "ingestiontime": 123456                                                                       
  }]
}

Table:

Create table events(id, timestamp, message, ingestion_time) 

我可以获得时间戳和消息值,但 ingestion_time 为 NULL。我正在尝试获取嵌套值并将纪元时间戳转换为日期时间。谢谢

更新:我正在获取所有值,但只插入了 1 条记录。尝试插入多个 rows/record.

插入 logging.events(timestamp,message,ingestion_time) select a.json#>>'{events,0,timestamp}', a.json#>>'{events,0,message}', to_timestamp(cast(a.json#>>'{events,0,ingestiontime}' AS BIGINT)) 来自 json_populate_recordset(NULL::logging.events, '[{
“时间戳”:1298734,
"message": "START RequestId: bjn937645",
“摄取时间”:123456
}, {
“时间戳”:1298734,
"message": "START RequestId: bjn937645",
“摄取时间”:123456
}]' :: jsonb) 作为 a

我尝试使用 json_populate_recordset。错误:函数 json_populate_recordset(reportinglogging.events, jsonb) 不存在..

insert into events (timestamp, message, ingestion_time)
select a.json#>>'{events,0,timestamp}', a.json#>>'{events,0,message}', to_timestamp(cast(a.json#>>'{events,0,ingestiontime}' AS integer))
  from (select
'{ 
"events": 
  [{                                                                                                   
     "timestamp": 1298734,                                                                                               
     "message": "START RequestId: bjn937645",                                                    
     "ingestiontime": 123456                                                                       
  }]
}' :: json AS json) AS a

查看 dbfiddle

中的测试
create or replace  function events_insert(p_object jsonb)
returns setof text AS
$body$
    declare
    p_json_value jsonb;
    v_rec record;
    v_timestamp timestamp;
    v_message text;
    v_time time WITHOUT TIME ZONE;
    v_id bigint;
    begin
    if (p_object['events'] ) is null then raise exception 'no working';
    else
        p_json_value := (p_object)['events'];
        raise info 'p_json_value: %', p_json_value::text;
        raise info 'p_json_value type: %', pg_typeof(p_json_value);
        for v_rec in (select  (jsonb_each_text (jsonb_array_elements
                                                    (p_json_value))).* )
        loop
            case
            when v_rec.key = 'timestamp' then v_timestamp := to_timestamp(v_rec.value::bigint);
            when v_rec.key = 'message' then v_message := v_rec.value;
            when v_rec.key = 'ingestiontime'
                then v_time :=  cast(to_char(v_rec.value::int,'FM999:99:99')as time WITHOUT TIME ZONE);
            else null;
            end case;
        end loop;
        insert into events(timestamp,message,ingestion_time)
                values(v_timestamp,v_message,v_time) returning id into v_id;
        return query select row_to_json(e.*)::text from events e where id = v_id;
    end if;
    end
$body$
language plpgsql;

称之为:

select * from events_insert(
'{"events":
      [{"timestamp": 1298734,
      "message": "START RequestId: bjn937645", 
        "ingestiontime": 123456}]}'::jsonb);

demo