{"id":2694,"date":"2018-11-06T23:31:13","date_gmt":"2018-11-06T23:31:13","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=2694"},"modified":"2018-11-07T13:58:00","modified_gmt":"2018-11-07T13:58:00","slug":"postgresql-to-manage-json-linux-hint","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/06\/postgresql-to-manage-json-linux-hint\/","title":{"rendered":"PostgreSQL to Manage JSON | Linux Hint"},"content":{"rendered":"<p>One of the many data types that PostgreSQL support is JSON. Since most of the web APIs communication uses JSON payload immensely, this feature is rather important. Rather than using the plaintext data type to store JSON objects, Postgres has a different data type which is optimized for JSON payloads, verifies that data stored in these fields confirms to the<\/p>\n<p><a href=\"https:\/\/tools.ietf.org\/html\/rfc7159\">RFC specification<\/a><\/p>\n<p>. Also in a classic Postgres manner, it allows you to fine-tune your JSON fields for maximum performance.<\/p>\n<p>While creating a table, you will have two options for your JSON column. Plain json data type and jsonb data type, both have their own advantages and disadvantages. We shall go through each of them, by creating a simple table with just 2 columns an ID and a JSON value. Following this we will query data from the table and get a feel for how to manage JSON formatted data inside Postgres.<\/p>\n<h2>JSON Data Type<\/h2>\n<h3>1. Creating a Table with JSON Data Type<\/h3>\n<p>Let\u2019s create a simple two column table named users:<\/p>\n<p>CREATE TABLE users (<br \/>\nid serial NOT NULL PRIMARY KEY,<br \/>\ninfo json NOT NULL<br \/>\n);<\/p>\n<p>Here the column id acts as the primary key, and it will increase in an incremental fashion thanks to the pseudotype serial so we won\u2019t have to worry about manually entering values for id as we go along.<\/p>\n<p>The second column is of json type and is forced to be NOT NULL. Let\u2019s enter a few rows of data to this table, consisting of JSON values.<\/p>\n<p>INSERT INTO users (info) VALUES (<br \/>\n\u2018{<br \/>\n&#8220;name&#8221;: &#8220;Jane Doe&#8221;,<br \/>\n&#8220;email&#8221;: &#8220;janedoe@example.com&#8221;,<br \/>\n&#8220;personalDetails&#8221;: {&#8220;age&#8221;:33, &#8220;gender&#8221;:&#8221;F&#8221;}<br \/>\n}\u2019);<\/p>\n<p>INSERT INTO users (info) VALUES (<br \/>\n\u2018{<br \/>\n&#8220;name&#8221;: &#8220;Jane Doe&#8221;,<br \/>\n&#8220;email&#8221;: &#8220;janedoe@example.com&#8221;,<br \/>\n&#8220;personalDetails&#8221;: {&#8220;age&#8221;:33, &#8220;gender&#8221;:&#8221;F&#8221;}<br \/>\n}\u2019);<\/p>\n<p>You can use your prefered <a href=\"https:\/\/linuxhint.com\/json_beautifier_ubuntu\/\">JSON beautifier\/minifier<\/a> to convert the JSON payloads above into a single line. So you can paste it at a go into your psql prompt.<\/p>\n<p>SELECT<\/p>\n<p>*<\/p>\n<p>FROM<\/p>\n<p>users;<\/p>\n<p>id<\/p>\n<p>|<\/p>\n<p>info<\/p>\n<p>&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;1 | {&#8220;name&#8221;: &#8220;John Doe&#8221;, &#8220;email&#8221;: &#8220;johndoe@example.com&#8221;&#8230;}<\/p>\n<p>2 | {&#8220;name&#8221;<\/p>\n<p>:<\/p>\n<p>&#8220;Jane Doe&#8221;<\/p>\n<p>,<\/p>\n<p>&#8220;email&#8221;<\/p>\n<p>:<\/p>\n<p>&#8220;janedoe@example.com&#8221;<\/p>\n<p>&#8230;<\/p>\n<p>}<br \/>\n(2 rows)<\/p>\n<p>The SELECT command at the end showed us that the rows were successfully inserted into the users table.<\/p>\n<h3>2. Querying JSON Data Type<\/h3>\n<p>Postgres allows you to dig into the JSON payload itself and retrieve a particular value out of it, if you reference it using the corresponding value. We can use the -&gt; operator after the json column\u2019s name, followed by the key inside the JSON object. Doing so<\/p>\n<p>For example, in the table we created above:<\/p>\n<p>SELECT<\/p>\n<p>info &#8211;<\/p>\n<p>&gt;<\/p>\n<p>\u2018email\u2019<\/p>\n<p>FROM<\/p>\n<p>users;<\/p>\n<p>&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\nid | ?column?<\/p>\n<p>&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-1 | &#8220;johndoe@example.com&#8221;<\/p>\n<p>2 | &#8220;janedoe@example.com&#8221;<\/p>\n<p>You may have noticed the double quotes in the column containing emails. This is because the -&gt; operator returns a JSON object, as present in the value of key \u201cemail\u201d. Of course, you can return just text, but you will have to use the -&gt;&gt; operator instead.<\/p>\n<p>SELECT<\/p>\n<p>info &#8211;<\/p>\n<p>&gt;&gt;<\/p>\n<p>\u2018email\u2019<\/p>\n<p>FROM<\/p>\n<p>users;<\/p>\n<p>id<\/p>\n<p>|<\/p>\n<p>?<\/p>\n<p>column<\/p>\n<p>?<\/p>\n<p>&#8212;-+&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-1 | johndoe@example.com<\/p>\n<p>2 |<\/p>\n<p>janedoe@example.com<\/p>\n<p>The difference between returning a JSON object and a string becomes clear once we start working with JSON objects nested inside other JSON objects. For example, I chose the \u201cpersonalDetails\u201d key to intentionally hold another JSON object. We can dig into this object too, if we want:<\/p>\n<p>SELECT<\/p>\n<p>info &#8211;<\/p>\n<p>&gt; &#8216;personalDetails&#8217;<\/p>\n<p>&#8211;<\/p>\n<p>&gt; &#8216;gender&#8217; FROM<\/p>\n<p>users;<\/p>\n<p>&nbsp;<\/p>\n<p>?<\/p>\n<p>column<\/p>\n<p>?<\/p>\n<p>&#8212;&#8212;&#8212;-&#8220;M&#8221;&#8221;F&#8221;(2 rows)<\/p>\n<p>This can let you go as deep into the JSON object as you would want to. Let\u2019s drop this table and create a new one (with the same name) but with JSONB type.<\/p>\n<h3>JSONB Data Type<\/h3>\n<p>Except for the fact that during creation of the table we mention jsonb data type instead of json, all else <em>looks<\/em> the same.<\/p>\n<p>CREATE TABLE users (<br \/>\nid serial NOT NULL PRIMARY KEY,<br \/>\ninfo jsonb NOT NULL<br \/>\n);<\/p>\n<p>Even the insertion of data and retrieval using the -&gt; operator behaves the same way. What has changed is all under the hood and noticeable in the table\u2019s performance. When converting JSON text into a jsonb, Postgres actually turns the various JSON value types into native Postgres type, so not all valid json objects can be saved as valid jsonb value.<\/p>\n<p>Moreover, jsonb doesn\u2019t preserve the whitespaces, order of json keys as supplied by the INSERT statement. Jsonb actually converts the payload into native postgres binary, hence the term <em>jsonb. <\/em><\/p>\n<p>Of course, insertion of jsonb datum has a performance overhead because of all these additional work that postgres needs to do. However, the advantage that you gain is in terms of faster processing of the already stored data, since your application would not have the need to parse a JSON payload everytime it retrieves one from the database.<\/p>\n<h3>JSON vs JSONB<\/h3>\n<p>The decision between json and jsonb sole depends on your use case. When in doubt use jsonb, since most applications tend to have more frequent read operations that write operations. On the other hand, if you are sure that your application is expected to do more synchronous write operations than read, then you may want to consider json as an alternative.<\/p>\n<h4>Conclusion<\/h4>\n<p>People working with JSON payloads and designing interfaces for Postgres storage will benefit immensely from <a href=\"https:\/\/www.postgresql.org\/docs\/10\/static\/datatype-json.html#JSON-KEYS-ELEMENTS\">this particular section <\/a>of their official documentation. The developers were kind enough to furnish us with jsonb indexing and other cool features which can be leveraged to improve the performance and simplicity of your application. I implore you to investigate these as well.<\/p>\n<p>Hopefully, you found this brief introduction of the matter helpful and inspiring.<\/p>\n<p><a href=\"https:\/\/linuxhint.com\/postgresql_json\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the many data types that PostgreSQL support is JSON. Since most of the web APIs communication uses JSON payload immensely, this feature is rather important. Rather than using the plaintext data type to store JSON objects, Postgres has a different data type which is optimized for JSON payloads, verifies that data stored in &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/11\/06\/postgresql-to-manage-json-linux-hint\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;PostgreSQL to Manage JSON | Linux Hint&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2694","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/2694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=2694"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/2694\/revisions"}],"predecessor-version":[{"id":2793,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/2694\/revisions\/2793"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=2694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=2694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=2694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}