0

I have a table with type varchar[]

Field Name Field Type
id int
data varchar[][]
insert_date timestamps

I have a data json object

{
 { 
  name : "Mr. A",
  class: 
  {
    class_id: 1,
    exam: 95,
  },
  {
    class_id: 2,
    exam: 78,
  }
 },
 { 
  name : "Mr. B",
  class: 
  {
    class_id: 1,
    exam: 87,
  },
  {
    class_id: 2,
    exam: 87,
  }
 }
}

how do i insert the data into 1 row?

1
  • 2
    De-normalizing using arrays (varchar[]) is pretty much always a bad idea. But it's totally unclear to me what exactly you are trying to store into the table. I can't relate any of the JSON keys to the columns in your table. Why don't you just store the JSON value into a jsonb column? Commented Aug 25, 2021 at 5:31

1 Answer 1

1

You should be use jsonb to store JSON data in your table.

CREATE TABLE customer {
  contact JSONB
}

Example(Object) :- Insert Object JSON Value

Insert into customer(contact )
Values('{ "phones":[ {"type": "mobile", "phone": "001001"} , {"type": "fix", "phone": "002002"} ] }')

Example(Array) :- Insert array JSON Value

Insert into customer(contact )
Values('[ {"type": "mobile", "phone": "001001"} , {"type": "fix", "phone": "002002"} ]')
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.