pg-format icon indicating copy to clipboard operation
pg-format copied to clipboard

Handling JSON where root element is an array

Open JoshuaHintze opened this issue 1 year ago • 3 comments

How would you handle the case of format.literal where you are trying to update a jsonb column but where the root element is an array?

For example, most JSON types you would insert would look like this

{
  "key": "value"
}

but there are times where you want the root element to be an array such as

[
  {
    "key": "value"
  }
]

However in this library and others i've seen with pg-format seem to always treat Array not as a JSON style object, but rather something to loop over and recursively call.

Do you have any thoughts?

JoshuaHintze avatar Nov 24 '24 19:11 JoshuaHintze

Hi @GimpMaster, can you add a failing test case for your scenario? I can see if I can add a fix then.

moltar avatar Nov 25 '24 09:11 moltar

Possibly related https://github.com/ScaleLeap/pg-format/issues/5

jcalfee avatar Feb 02 '25 23:02 jcalfee

Sorry, I forgot I commented on this. @jcalfee - your post is almost 2 years old. I forgot how I worked around this. I'll try to remember later to figure out what I did.

JoshuaHintze avatar Feb 03 '25 04:02 JoshuaHintze