Introduction to PostgreSQL JSON vs JSONNB
PostgreSQL JSON where JSON stands for ‘Javascript Object Notation’ is a data type of JSON in PostgreSQL which is used to store the data as text with the values stored according to the JSON rules. It basically does not perform any extra processing, instead copies the data similar to the JSON input text, and the processing function is reparsed in every execution in the case of JSON.
PostgreSQL jsonb where jsonb stands for ‘Javascript Object Notation Binary’ is a data type of JSON in PostgreSQL which is used to store the data in the binary format. JSON data is first converted and then stored, so the input of data is somewhat slower than JSON but the processing speed in binary form is much higher so the overall efficiency is excellent in the case of JSON data type.
Head to Head Comparison Between PostgreSQL JSON vs JSONNB (Infographics)
Below are the top 9 differences between PostgreSQL JSON vs JSONNB:
Comparison Table of PostgreSQL JSON vs JSONNB
Below given is the comparison table showing the head to head comparison between JSON and jsonb in PostgreSQL:
S.No. |
PostgreSQL json |
PostgreSQL jsonb |
1 | JSON data type stores the exact copy of input text in JSON. | Jsonb stores the data as binary code. Basically, it stores the data in binary form which is not an ASCII/ UTF-8 string. |
2 | Json preserves the original formatting like the whitespaces as well as the ordering of keys. | Jsonb does not preserve the original formatting of text like the whitespaces and the ordering of keys. |
3 | Json processes input faster than jsonb as there is no conversion involved in this. | Jsonb converts the JSON data into the binary form so it has slightly slower input due to the binary conversion overhead. |
4 | There is no change in the Schema design while working with JSON. All the entities, attributes, values remain the same. | Schema designs are comparatively easier than Json as it replaces the entity- attribute value with jsonb blob columns which are easier to query. |
5 | JSON data does not take much disk space as the JSON data is stored as it is. | More disk space is required for jsonb data as compared to JSON data type. |
6 | Processing function needs to be reparsed in each execution when the input text is in JSON data type. | In the case of jsonb, it is comparatively faster to process the data as no reparsing is needed. |
7 | JSON does not support indexing, unlike jsonb. | Jsonb supports indexing to search for the keys or key/ value pairs which is a great advantage at a bigger database jsonb documents. |
8 | In JSON, if there exists more than one key for the given value, all the key/value pairs are preserved. | Jsonb does not preserve duplicate object keys. In case of a duplicate, only the last key/value pair is preserved. |
9 | Unicode escapes are allowed in the JSON input function regardless of the data encoding. It only checks for syntactic errors. | The input function of jsonb is stricter than the JSON as it does not allow the unicode escapes for non-ASCII characters if the database encoding of UTF8 is not set. |
Key Differences of PostgreSQL JSON vs JSONNB
Some of the key differences between JSON and jsonb explaining the detailed differences between the two are given below:
1. One simple difference between JSON and jsonb data type is that JSON stores the exact copy of the data represented/ inputted in the JSON format to the user whereas jsonb stores the data in the binary format which means that the input data is first processed and then stored in the binary form.
2. If we talk about the ingesting speed and processing speed, it is obvious that the ingesting data using jsonb would be somewhat slower as the data is stored in the binary format (so it will be processed first before getting stored) but the further processing of jsonb data is quite faster as compared to JSON. So in case when there is a need only to ingest too much data, JSON data type would work great.
3. Besides the efficiency of data performance, Jsonb also provides the benefit of GIN (Generalised Inverted Index). It allows the testing of Containment, which checks whether one document is contained in another document or not with the help of @> operator. It returns the result as true or false. Another feature is the provision of Existence Operator in jsonb which checks the existence of top-level keys in JSONB. It is an important feature provided by jsonb and cannot be done using JSON.
4. One of the important improvements of jsonb over JSON is that it allows the indexing of JSON data which is quite helpful in retrieving speedy and correct results especially in the case of aggregate functions when there are millions and trillions of data in the database.
5. jsonb does not store the duplicate key/ value pairs which can be good or bad depending on the requirement of the user. Unlike JSON which stores all the key/ value pairs, it only stores the last key/value pairs.
6. One major drawback of using the jsonb in PostgreSQL is that PostgreSQL does not maintain the statistics while using the jsonb columns. Generally, proper statistics are maintained for each table by PostgreSQL like the distinct values seen, most common values, NULL values, histograms of the distributions. Using the above data, the query planner in PostgreSQL makes the statistics of which plan will provide the best results. But for the jsonb columns, no statistics are stored which results in no planning and ultimately poor results.
7. One another problem encountered with the JSON data type is that at last jsonb results in larger storage footprints as compared to JSON because the key names in JSON are not deduplicated in the case of jsonb storage. Moreover, jsonb encoding of input text has a bit overhead as it does not parse the JSON in order to retrieve a particular field. So the database stores the key/ value pair for each row which increases the database size. If the key appears frequently in the jsonb blobs, one solution to the problem is to store the key as a column in the table.
Conclusion
Above description clearly explains what the JSON and jsonb data type is and how they work in PostgreSQL. As both the JSON and jsonb are used to store the data in their respective purpose, so while working with PostgreSQL, it is important for the programmer to understand the difference between the two and use them accordingly.
Recommended Articles
This is a guide to PostgreSQL JSON vs JSONNB. Here we discuss the PostgreSQL JSON vs JSONNB key differences with infographics and comparison table. You may also have a look at the following articles to learn more –