0

I have a table with a column of type jsonb called attributes.

I have rows with values like -

{
    "id1": "something1",
    "id2": "something2",
    "id3": "something3",
}

and I want to convert them into -

{
    "id1": ["something1"],
    "id2": ["something2"],
    "id3": ["something3"],
}

How can I do that?

1

1 Answer 1

1

You would have to unroll it with jsonb_to_record or write a function in plv8,

SELECT jsonb_build_object(
  'id1', jsonb_build_array(id1),
  'id2', jsonb_build_array(id2),
  'id3', jsonb_build_array(id3)
) 
FROM (VALUES ($${
  "id1": "something1",
  "id2": "something2",
  "id3": "something3"
}$$::jsonb)) AS t(j)
CROSS JOIN jsonb_to_record(j)
  AS t2(id1 text, id2 text, id3 text);

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.