donk68254 2018-06-28 03:00
浏览 849
已采纳

如何将Json数据导入Postgres表

I have an API that gives json data text file called corporateHoliday.txt.

The data look like this:

{
    "holiday_id":"1712010100000104",
    "holiday_date":"2018-12-30 00:00:00.0",
    "holiday_description":"SUNDAY",
    "holiday_status":"A"
},
{
    "holiday_id":"1712010100000103",
    "holiday_date":"2018-12-29 00:00:00.0",
    "holiday_description":"SATURDAY",
    "holiday_status":"A"
}

My postgres table attributes look like this t_leave_holida(objectid, holiday_date,description,recstatus)

I want to import these json data from txt file into the postgres database table?

  • 写回答

2条回答 默认 最新

  • dongluan0020 2018-06-28 04:03
    关注

    you can use jsonb type and just select data from json, like here:

    db=# create table t_leave_holida(objectid bigint, holiday_date timestamp(1), description text,recstatus text);
    db=# insert into t_leave_holida select (d->>'holiday_id')::bigint, (d->>'holiday_date')::timestamp, d->>'holiday_description', d->>'holiday_status' from (select '{
        "holiday_id":"1712010100000104",
        "holiday_date":"2018-12-30 00:00:00.0",
        "holiday_description":"SUNDAY",
        "holiday_status":"A"
    }'::jsonb d) ali;
    INSERT 0 1
    db=# select * from t_leave_holida;
         objectid     |    holiday_date     | description | recstatus
    ------------------+---------------------+-------------+-----------
     1712010100000104 | 2018-12-30 00:00:00 | SUNDAY      | A
    (1 row)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题