1

I am trying to insert bulk data into postgres. So I have converted my data into json object and now I want to insert that data into a physical table the json object can contain over 100k records. So I want to do it dynamically by using postgres function. My question is how can I query my sample data as a table

This is the sample json data

[
  {
    "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
  }
]
James Z
  • 12,209
  • 10
  • 24
  • 44
Kovid Purohit
  • 258
  • 1
  • 4
  • 15

1 Answers1

1

You can use create a table (e.g.tab) with json column (e.g.jsondata) and by using json_array_elements() function :

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

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55