{"id":2123,"date":"2025-02-16T16:23:47","date_gmt":"2025-02-16T09:23:47","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=2123"},"modified":"2025-02-16T16:31:26","modified_gmt":"2025-02-16T09:31:26","slug":"postgresql-json","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-json\/","title":{"rendered":"PostgreSQL JSON"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: In this tutorial, you&#8217;ll learn how to use the PostgreSQL JSON data types to store JSON data in the databases. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='json-overview'>JSON Overview <a href=\"#json-overview\" class=\"anchor\" id=\"json-overview\" title=\"Anchor for JSON Overview\">#<\/a><\/h2>\n\n\n\n<p>JSON, or JavaScript Object Notation, is a lightweight data-interchange format that is simple for developers to work with and easy for computers to manage.<\/p>\n\n\n\n<p>Here are the key features of JSON:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Human-readable<\/strong>: JSON uses a simple text format.<\/li>\n\n\n\n<li><strong>Lightweight<\/strong>: JSON has a minimal syntax and structure.<\/li>\n\n\n\n<li><strong>Structured data<\/strong>: JSON uses arrays and objects to represent data.<\/li>\n\n\n\n<li><strong>Language-dependent<\/strong>: JSON works seamlessly with many programming languages.<\/li>\n<\/ul>\n\n\n\n<p>The following are typical scenarios where you find JSON useful:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>RESTful APIs<\/li>\n\n\n\n<li>Configuration files (.json format)<\/li>\n\n\n\n<li>Data storage<\/li>\n<\/ul>\n\n\n\n<p>JSON uses two structures to store data:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Objects<\/strong>: A collection of key-value pairs enclosed in curly braces (<code>{}<\/code>).<\/li>\n\n\n\n<li><strong>Arrays<\/strong>: An order list of values enclosed in square brackets (<code>[]<\/code>).<\/li>\n<\/ul>\n\n\n\n<p>Objects and arrays can be nested and mixed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='objects'>Objects <a href=\"#objects\" class=\"anchor\" id=\"objects\" title=\"Anchor for Objects\">#<\/a><\/h3>\n\n\n\n<p>A JSON object is a list of key-value pairs enclosed in curly braces. Each pair consists of:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>A key<\/strong>: appears within double quotes (<code>\"<\/code>).<\/li>\n\n\n\n<li><strong>A value<\/strong>: is any valid JSON value such as a number, a string, an object, an array, etc.<\/li>\n\n\n\n<li>A colon (<code>:<\/code>) separates the key and value.<\/li>\n<\/ul>\n\n\n\n<p>For example, the following shows a JSON object:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">{<span class=\"hljs-attr\">\"product_name\"<\/span>: <span class=\"hljs-string\">\"iPhone 16\"<\/span>, <span class=\"hljs-attr\">\"price\"<\/span>: <span class=\"hljs-number\">1299.99<\/span> }<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The JSON object has two keys, <code>product_name<\/code>, and <code>price<\/code>, with corresponding values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='arrays'>Arrays <a href=\"#arrays\" class=\"anchor\" id=\"arrays\" title=\"Anchor for Arrays\">#<\/a><\/h3>\n\n\n\n<p>A JSON array is an ordered list of values enclosed in square brackets (<code>[]<\/code>). The values can have different types.<\/p>\n\n\n\n<p>For example, the following is a JSON array that stores the product features:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json\">&#91;<span class=\"hljs-string\">\"Camera\"<\/span>, <span class=\"hljs-string\">\"Face Recognition\"<\/span>, <span class=\"hljs-string\">\"AI\"<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='json-data-types'>JSON Data Types <a href=\"#json-data-types\" class=\"anchor\" id=\"json-data-types\" title=\"Anchor for JSON Data Types\">#<\/a><\/h3>\n\n\n\n<p>Besides object and array, JSON supports the following simple data types:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>string<\/li>\n\n\n\n<li>number<\/li>\n\n\n\n<li>boolean<\/li>\n\n\n\n<li>null<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-json-data-types'>PostgreSQL JSON data types <a href=\"#postgresql-json-data-types\" class=\"anchor\" id=\"postgresql-json-data-types\" title=\"Anchor for PostgreSQL JSON data types\">#<\/a><\/h2>\n\n\n\n<p>PostgreSQL has two built-in data types for storing JSON:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>JSON<\/strong>: stores an exact copy of JSON data.<\/li>\n\n\n\n<li><strong>JSONB<\/strong>: stores the JSON data in binary format.<\/li>\n<\/ul>\n\n\n\n<p>The following table shows the key differences between <code>JSON<\/code> and <code>JSONB<\/code> types in PostgreSQL:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>JSON<\/th><th>JSONB<\/th><\/tr><\/thead><tbody><tr><td>Storage<\/td><td>Text<\/td><td>Binary storage format<\/td><\/tr><tr><td>Size<\/td><td>Bigger because PostgreSQL has to retain whitespace.<\/td><td>Smaller<\/td><\/tr><tr><td>Indexing<\/td><td>Full-text search indexes<\/td><td>Binary indexes<\/td><\/tr><tr><td>Query performance<\/td><td>Slower due to parsing<\/td><td>Faster due to binary storage<\/td><\/tr><tr><td>Parsing<\/td><td>Parse each time<\/td><td>Parse once, store in binary format<\/td><\/tr><tr><td>Ordering of keys<\/td><td>Preserved<\/td><td>Not preserved<\/td><\/tr><tr><td>Duplicate keys<\/td><td>Allow duplicate key, the last value is retained<\/td><td>Do not allow duplicate keys.<\/td><\/tr><tr><td>Use cases<\/td><td>Only if you want to preserve the order of the keys.<\/td><td>Storing JSON documents where fast querying and indexing are required<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>PostgreSQL recommends using the <code>JSONB<\/code> data type for smaller storage and better query efficiency. PostgreSQL offers the <code>JSONPATH<\/code> data type to make querying JSON data more efficient.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='postgresql-jsonb-example'>PostgreSQL JSONB example <a href=\"#postgresql-jsonb-example\" class=\"anchor\" id=\"postgresql-jsonb-example\" title=\"Anchor for PostgreSQL JSONB example\">#<\/a><\/h2>\n\n\n\n<p>First, <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-table\/\">create a new table<\/a> called <code>phones<\/code> to store phone data:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> phones (\n  id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n  <span class=\"hljs-type\">name<\/span> <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n  properties <span class=\"hljs-type\">JSONB<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=Q1JFQVRFIFRBQkxFIHBob25lcyAoIGlkIElOVCBHRU5FUkFURUQgQUxXQVlTIEFTIElERU5USVRZIFBSSU1BUlkgS0VZLCBuYW1lIFZBUkNIQVIoMjU1KSBOT1QgTlVMTCwgcHJvcGVydGllcyBKU09OQiApOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The data type of the <code>properties<\/code> column is <code>JSONB<\/code>.<\/p>\n\n\n\n<p>Second, <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-insert\/\">insert three rows<\/a> with JSON data into the <code>phones<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> phones(<span class=\"hljs-type\">name<\/span>, properties ) <span class=\"hljs-keyword\">VALUES<\/span>\n(<span class=\"hljs-string\">'iPhone 16 Pro'<\/span>, \n <span class=\"hljs-string\">'{\n    \"display\": \"6.1-inch OLED\",\n    \"features\": &#91;\"Face ID\", \"ProMotion 120Hz\", \"MagSafe\", \"iOS 18\"]\n }'<\/span>),\n\n(<span class=\"hljs-string\">'Galaxy S23 Ultra'<\/span>, \n <span class=\"hljs-string\">'{\n    \"display\": \"6.8-inch AMOLED\",\n    \"features\": &#91;\"S Pen\", \"120Hz Display\", \"One UI 5\", \"Wireless Charging\"]\n }'<\/span>),\n\n(<span class=\"hljs-string\">'Pixel 7 Pro'<\/span>, \n <span class=\"hljs-string\">'{\n    \"display\": \"6.7-inch OLED\",\n    \"features\": &#91;\"Magic Eraser\", \"Pure Android Experience\", \"Face Unlock\", \"5G Connectivity\"]\n }'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=SU5TRVJUIElOVE8gcGhvbmVzKG5hbWUsIHByb3BlcnRpZXMgKSBWQUxVRVMgKCdpUGhvbmUgMTYgUHJvJywgJ3sgImRpc3BsYXkiOiAiNi4xLWluY2ggT0xFRCIsICJmZWF0dXJlcyI6IFsiRmFjZSBJRCIsICJQcm9Nb3Rpb24gMTIwSHoiLCAiTWFnU2FmZSIsICJpT1MgMTgiXSB9JyksICgnR2FsYXh5IFMyMyBVbHRyYScsICd7ICJkaXNwbGF5IjogIjYuOC1pbmNoIEFNT0xFRCIsICJmZWF0dXJlcyI6IFsiUyBQZW4iLCAiMTIwSHogRGlzcGxheSIsICJPbmUgVUkgNSIsICJXaXJlbGVzcyBDaGFyZ2luZyJdIH0nKSwgKCdQaXhlbCA3IFBybycsICd7ICJkaXNwbGF5IjogIjYuNy1pbmNoIE9MRUQiLCAiZmVhdHVyZXMiOiBbIk1hZ2ljIEVyYXNlciIsICJQdXJlIEFuZHJvaWQgRXhwZXJpZW5jZSIsICJGYWNlIFVubG9jayIsICI1RyBDb25uZWN0aXZpdHkiXSB9Jyk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Third, retrieve data from the <code>phones<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  id,\n  <span class=\"hljs-type\">name<\/span>,\n  properties\n<span class=\"hljs-keyword\">FROM<\/span>\n  phones;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIGlkLCBuYW1lLCBwcm9wZXJ0aWVzIEZST00gcGhvbmVzOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> id |       <span class=\"hljs-type\">name<\/span>       |                                                       properties\n\n<span class=\"hljs-comment\">----+------------------+-------------------------------------------------------------------------------------------------------------------------<\/span>\n  <span class=\"hljs-number\">1<\/span> | iPhone <span class=\"hljs-number\">16<\/span> Pro    | {\"display\": \"6.1-inch OLED\", \"features\": &#91;\"Face ID\", \"ProMotion 120Hz\", \"MagSafe\", \"iOS 18\"]}\n  <span class=\"hljs-number\">2<\/span> | Galaxy S23 Ultra | {\"display\": \"6.8-inch AMOLED\", \"features\": &#91;\"S Pen\", \"120Hz Display\", \"One UI 5\", \"Wireless Charging\"]}\n  <span class=\"hljs-number\">3<\/span> | Pixel <span class=\"hljs-number\">7<\/span> Pro      | {\"display\": \"6.7-inch OLED\", \"features\": &#91;\"Magic Eraser\", \"Pure Android Experience\", \"Face Unlock\", \"5G Connectivity\"]}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fourth, retrieve the phones with the <code>display<\/code> key:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  id,\n  <span class=\"hljs-type\">name<\/span>,\n  properties -&gt; <span class=\"hljs-string\">'display'<\/span> <span class=\"hljs-keyword\">AS<\/span> display\n<span class=\"hljs-keyword\">FROM<\/span>\n  phones;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIGlkLCBuYW1lLCBwcm9wZXJ0aWVzIC0%2BICdkaXNwbGF5JyBBUyBkaXNwbGF5IEZST00gcGhvbmVzOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> id |       <span class=\"hljs-type\">name<\/span>       |      display\n<span class=\"hljs-comment\">----+------------------+-------------------<\/span>\n  <span class=\"hljs-number\">1<\/span> | iPhone <span class=\"hljs-number\">16<\/span> Pro    | \"6.1-inch OLED\"\n  <span class=\"hljs-number\">2<\/span> | Galaxy S23 Ultra | \"6.8-inch AMOLED\"\n  <span class=\"hljs-number\">3<\/span> | Pixel <span class=\"hljs-number\">7<\/span> Pro      | \"6.7-inch OLED\"<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we use the operator <code>-&gt;<\/code> to extract a JSON value by a key. A JSON value is surrounded by double quotes.<\/p>\n\n\n\n<p>To return a JSON value as text, you use the operator <code>-&gt;&gt;<\/code>. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  id,\n  <span class=\"hljs-type\">name<\/span>,\n  properties -&gt;&gt; <span class=\"hljs-string\">'display'<\/span> <span class=\"hljs-keyword\">AS<\/span> display\n<span class=\"hljs-keyword\">FROM<\/span>\n  phones;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIGlkLCBuYW1lLCBwcm9wZXJ0aWVzIC0%2BPiAnZGlzcGxheScgQVMgZGlzcGxheSBGUk9NIHBob25lczs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> id |       <span class=\"hljs-type\">name<\/span>       |     display\n<span class=\"hljs-comment\">----+------------------+-----------------<\/span>\n  <span class=\"hljs-number\">1<\/span> | iPhone <span class=\"hljs-number\">16<\/span> Pro    | <span class=\"hljs-number\">6.1<\/span>-inch OLED\n  <span class=\"hljs-number\">2<\/span> | Galaxy S23 Ultra | <span class=\"hljs-number\">6.8<\/span>-inch AMOLED\n  <span class=\"hljs-number\">3<\/span> | Pixel <span class=\"hljs-number\">7<\/span> Pro      | <span class=\"hljs-number\">6.7<\/span>-inch OLED<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, retrieve the main feature of each phone:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  id,\n  <span class=\"hljs-type\">name<\/span>,\n  properties -&gt; <span class=\"hljs-string\">'features'<\/span> -&gt;&gt; <span class=\"hljs-number\">0<\/span> <span class=\"hljs-keyword\">AS<\/span> main_feature\n<span class=\"hljs-keyword\">FROM<\/span>\n  phones;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?q=U0VMRUNUIGlkLCBuYW1lLCBwcm9wZXJ0aWVzIC0%2BICdmZWF0dXJlcycgLT4%2BIDAgQVMgbWFpbl9mZWF0dXJlIEZST00gcGhvbmVzOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"> id |       <span class=\"hljs-type\">name<\/span>       | main_feature\n<span class=\"hljs-comment\">----+------------------+--------------<\/span>\n  <span class=\"hljs-number\">1<\/span> | iPhone <span class=\"hljs-number\">16<\/span> Pro    | Face ID\n  <span class=\"hljs-number\">2<\/span> | Galaxy S23 Ultra | S Pen\n  <span class=\"hljs-number\">3<\/span> | Pixel <span class=\"hljs-number\">7<\/span> Pro      | Magic Eraser<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>properties-&gt;'features'<\/code> returns a JSON array.<\/li>\n\n\n\n<li>The <code>properties-&gt;'features' -&gt;&gt; 0<\/code> returns the first element of the JSON array as text.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use JSONB data type to store data.<\/li>\n\n\n\n<li>Use the operator <code>-&gt;<\/code> to extract a JSON value from a JSON object by a key.<\/li>\n\n\n\n<li>Use the operator <code>-&gt;&gt;<\/code> to extract a JSON value as text.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2123\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-json\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL JSON\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2123\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-json\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL JSON\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: In this tutorial, you&#8217;ll learn how to use the PostgreSQL JSON data types to store JSON data in the databases. JSON Overview # JSON, or JavaScript Object Notation, is a lightweight data-interchange format that is simple for developers to work with and easy for computers to manage. Here are the key features of JSON: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":13,"menu_order":95,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2123","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PostgreSQL JSON<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to use the PostgreSQL JSON data types to store JSON data in the databases.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-json\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL JSON\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to use the PostgreSQL JSON data types to store JSON data in the databases.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-json\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-16T09:31:26+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-json\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-json\\\/\",\"name\":\"PostgreSQL JSON\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2025-02-16T09:23:47+00:00\",\"dateModified\":\"2025-02-16T09:31:26+00:00\",\"description\":\"In this tutorial, you'll learn how to use the PostgreSQL JSON data types to store JSON data in the databases.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-json\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-json\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-json\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Tutorial\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL JSON\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PostgreSQL JSON","description":"In this tutorial, you'll learn how to use the PostgreSQL JSON data types to store JSON data in the databases.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-json\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL JSON","og_description":"In this tutorial, you'll learn how to use the PostgreSQL JSON data types to store JSON data in the databases.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-json\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-02-16T09:31:26+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-json\/","url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-json\/","name":"PostgreSQL JSON","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2025-02-16T09:23:47+00:00","dateModified":"2025-02-16T09:31:26+00:00","description":"In this tutorial, you'll learn how to use the PostgreSQL JSON data types to store JSON data in the databases.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-json\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-json\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-json\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Tutorial","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL JSON"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=2123"}],"version-history":[{"count":4,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2123\/revisions"}],"predecessor-version":[{"id":2130,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/2123\/revisions\/2130"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=2123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}