Differences between json and jsonb - JSON data type in PostgreSQL
You may have read differences between PostgreSQL datatypes JSON and JSONB. If not, let see the difference between both theoretically and practically.
Although, both json and jsonb are types of PostgreSQL JSON datatypes and accepts almost same format as input values i.e. json. But still there are some difference which one must know to efficiently use or can choose from both json and jsonb.
The main different is how they actually store data; the json data type stores input value as it is but jsonb stores value in a decomposed binary format which lead to difference in efficiency.
Jsonb is slower as compare to json while saving the input values because of converting json values to binary format before saving.
Jsonb fast in processing the data because no re-parsing needed as compare to json datatype which needs processing function to re-parsing on every execution.
Few other differences between json and jsonb are:
- Jsonb supports indexing which is advantage over json data type.
- As json stores the value as it is, it keeps un-neccessary white space between tokens while jsonb convert json data to binary format it doesn't preserve the white-space.
- Json keeps the order of keys in json object, Jsonb doesn't keeps order same as we pass the value.
- As json store value as it is, so it keep duplicate keys too if input value have same key multiple times, But jsonb keeps last key-value pair in-case input have repeating key (because of conversion).
So from above, you got the idea which is better to use json or jsonb? I believe your answer will be jsonb. Of course! it has few more advantages over jsonb.
How you decide practically, which one should use from json or jsonb?
From practical point of view, I have observed one should you JSON data type when:- You have do mostly read column as it is, no where clause e.g. saving data in for event logs
- You have 1 dimensional associative Array or object and have to apply operations while reading column data
One should you JSONB data type when:
- You have 2-dimensional associative array or object and have to apply operation while reading column data
- You have array of associative arrays or objects and have to apply operation while reading column data
Comments
Post a Comment