Home > Net >  malformed array literal when convetring jsonb array of jsonb items to postgres array of jsonb by jso
malformed array literal when convetring jsonb array of jsonb items to postgres array of jsonb by jso

Time:06-15

I have jsonb-array:

element_values := '[
  {
    "element_id": "a7993f3d-9256-4354-a147-5b9d18d7812b", 
    "value": true
  }, 
  {
    "element_id": "ceeb364e-bb88-4f41-9c56-9e5f4d0bc1fb", 
    "value": None
  },
  ...
]'::JSONB

And I want to convert it into array of jsonb objects: JSONB[]

I tried this method:

<<elements_n_values_relationship_create>>
DECLARE
    elements_n_values_relationship JSONB[];

BEGIN
    SELECT * FROM jsonb_array_elements(element_values) INTO elements_n_values_relationship;
    ...
END;

But I got the following error:

 ERROR:  malformed array literal: "{"value": true, "element_id": "a7993f3d-9256-4354-a147-5b9d18d7812b"}"
 DETAIL:  Unexpected array element.

Why it does not work?

CodePudding user response:

You have to use null in place of None to make your statement work

EDIT: Try this in pgadmin or any SQL client, is is working as expected

select   jsonb_array_elements('[{
  "element_id": "a7993f3d-9256-4354-a147-5b9d18d7812b", 
  "value": true
}, 
{
  "element_id": "ceeb364e-bb88-4f41-9c56-9e5f4d0bc1fb", 
  "value": null
}]'::JSONB);
jsonb_array_elements
"{""value"":"{""value"": true, ""element_id"": ""a7993f3d-9256-4354-a147-5b9d18d7812b""}"
{ "value": null, "element_id": "ceeb364e-bb88-4f41-9c56-9e5f4d0bc1fb" }
  • Related