9

Is there any reason to use array of JSONB values instead of just raw JSONB?

My use case is that I need to store custom forms definition in list of JSON objects. And my question is whether it would be better to store it as raw JSON value or split this into separate JSONB objects.

What are pros and cons of each solution?

asked Feb 14, 2017 at 13:10

1 Answer 1

8

Generally, if you have an array of one thing (statically typed), it's better to store them as jsonb[] (a native SQL array). There are far more functions that operate over a SQL arrays, and indexes. It also tends to less overhead in storage. JSONb only has,

jsonb_array_length(jsonb)
jsonb_array_elements(jsonb)
jsonb_array_elements_text(jsonb)

While Array has a far more, but most of those features are implemented as operators.

Ultimately, in most use cases it won't matter much. You're better off normalizing your data. You really shouldn't be storing multiple json documents in one row. That sounds like a big "con" to begin with.

answered Feb 14, 2017 at 17:48
1
  • Normalization isn't option in that case as this is highly user dependent array that contains forms' fields' description. So normalization doesn't even make sense as all fields will be similar, but not the same for each row. Commented Feb 17, 2017 at 21:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.