代码之家  ›  专栏  ›  技术社区  ›  Kovid Purohit

如何将json对象转换成postgres表

  •  0
  • Kovid Purohit  · 技术社区  · 5 年前

    这是示例json数据

    [
      {
        "sequence": 123123.0,
        "Card number ": "12312qwe",
        "Tracking number": 1231233.0,
        "Expiry Date": 43741.0
      },
      {
        "sequence": 123123.0,
        "Card number ": "12312qwe",
        "Tracking number": 1231233.0,
        "Expiry Date": 43741.0
      },
     {
        "sequence": 123123.0,
        "Card number ": "12312qwe",
        "Tracking number": 1231233.0,
        "Expiry Date": 43741.0
      }
    ]
    
    0 回复  |  直到 5 年前
        1
  •  1
  •   Barbaros Özhan    5 年前

    你可以使用创建一个表(例如。 tab )与 json 列(例如。 jsondata )通过使用 json_array_elements()

    create table tab as
    with tab as
    (
     select '[
      {
        "sequence": 123123.0,
        "Card number ": "12312qwe",
        "Tracking number": 1231233.0,
        "Expiry Date": 43741.0
      },
      {
        "sequence": 123123.0,
        "Card number ": "12312qwe",
        "Tracking number": 1231233.0,
        "Expiry Date": 43741.0
      },
     {
        "sequence": 123123.0,
        "Card number ": "12312qwe",
        "Tracking number": 1231233.0,
        "Expiry Date": 43741.0
      }
    ]'::json as jsondata
    )
    select js ->> 'sequence' as sequence, js ->> 'Card number ' as Cardnumber, 
           js ->> 'Tracking number' as Trackingnumber, js ->> 'Expiry Date' as ExpiryDate
      from
      (
        select json_array_elements(jsondata) as js
          from tab
      ) q1 
    

    Demo