0

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?

2
  • Enclose None in double quotes: "value": "None". Commented Jun 14, 2022 at 12:21
  • @Benilson problem is not here. Even I have jsonb only with first item, I get error Commented Jun 14, 2022 at 12:39

1 Answer 1

2

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" }
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, it is miss print. I copied json from python. Asyncpg will replace None to null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.