-1

I have an array of json Objects in following format:

[
  {
    product: {
      id: 24,      
      raisedby: 'abc@testcom'
    }
  },
  {
    product: {
      id: 12,      
      raisedby: 'xyz@testcom'
    }
  },
  {
    product: {
      id: 9,      
      raisedby: 'abcde@testcom'
    }
  }
]

I am trying to build a Postgres Insert Statement as follows but it does not work. Any help appreciated

insert into public.redeem_request(redeem_product_id, redeem_raisedby) 
select (e->> 'product'->> 'id')::int, 
        e->>'product' ->> 'raisedby' 
from jsonb_array_elements($1) as t(e) 
returning *
New contributor
Joy is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
9
  • e ->> 'product' needs to be e -> 'product' Jun 22 at 12:35
  • Tried that as well getting Token "product" is invalid.
    – Joy
    Jun 22 at 12:37
  • 2
    Works just fine: dbfiddle.uk/… (assuming your invalid JSON in the question is just lazyness when constructing the question) Jun 22 at 12:40
  • please note my properties do not have quotes. it is product not "product"
    – Joy
    Jun 22 at 13:07
  • 3
    Then it's invalid JSON and you can not process that with any of the Postgres JSON functions. Jun 22 at 13:15

0

Browse other questions tagged or ask your own question.