<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>json_agg &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/json_agg/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Wed, 11 Oct 2017 09:51:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>PostgreSQL: How to convert Table Data into JSON formatted Data?</title>
		<link>https://www.dbrnd.com/2016/08/postgresql-how-to-convert-table-data-record-into-json-formatted-document-data/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Fri, 05 Aug 2016 05:38:22 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[json_agg]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[row_to_json]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=1809</guid>

					<description><![CDATA[PostgreSQL 9.4 introduced very powerful data type called JSON data type. It also introduced a variety of new operators and functions related to JSON data type. This is one of the interesting topics for us and I have recently published a series of articles on PostgreSQL JSON Data type. You can visit related articles here: [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-how-to-convert-table-data-record-into-json-formatted-document-data%2F&amp;linkname=PostgreSQL%3A%20How%20to%20convert%20Table%20Data%20into%20JSON%20formatted%20Data%3F" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-how-to-convert-table-data-record-into-json-formatted-document-data%2F&amp;linkname=PostgreSQL%3A%20How%20to%20convert%20Table%20Data%20into%20JSON%20formatted%20Data%3F" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-how-to-convert-table-data-record-into-json-formatted-document-data%2F&amp;linkname=PostgreSQL%3A%20How%20to%20convert%20Table%20Data%20into%20JSON%20formatted%20Data%3F" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-how-to-convert-table-data-record-into-json-formatted-document-data%2F&amp;linkname=PostgreSQL%3A%20How%20to%20convert%20Table%20Data%20into%20JSON%20formatted%20Data%3F" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-how-to-convert-table-data-record-into-json-formatted-document-data%2F&amp;linkname=PostgreSQL%3A%20How%20to%20convert%20Table%20Data%20into%20JSON%20formatted%20Data%3F" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-how-to-convert-table-data-record-into-json-formatted-document-data%2F&amp;linkname=PostgreSQL%3A%20How%20to%20convert%20Table%20Data%20into%20JSON%20formatted%20Data%3F" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F08%2Fpostgresql-how-to-convert-table-data-record-into-json-formatted-document-data%2F&#038;title=PostgreSQL%3A%20How%20to%20convert%20Table%20Data%20into%20JSON%20formatted%20Data%3F" data-a2a-url="https://www.dbrnd.com/2016/08/postgresql-how-to-convert-table-data-record-into-json-formatted-document-data/" data-a2a-title="PostgreSQL: How to convert Table Data into JSON formatted Data?"></a></p><p>PostgreSQL 9.4 introduced very powerful data type called JSON data type. It also introduced a variety of new operators and functions related to JSON data type.</p>
<blockquote><p>This is one of the interesting topics for us and I have recently published a series of articles on PostgreSQL JSON Data type.</p></blockquote>
<p><strong>You can visit related articles here:</strong></p>
<p><strong><a href="https://www.dbrnd.com/2016/02/postgresql-9-4-the-json-data-type-is-awesome-part-1/" target="_blank">PostgreSQL 9.4: The JSON data type is Awesome (Part 1/3).</a><br />
<a href="https://www.dbrnd.com/2016/02/postgresql-9-4-introduced-json-functions-and-operators-part-2/" target="_blank">PostgreSQL 9.4: Introduced JSON Functions and Operators (Part 2/3).</a><br />
<a href="https://www.dbrnd.com/2016/02/postgresql-9-4-indexing-on-jsonb-data-type-part-3/" target="_blank">PostgreSQL 9.4: Indexing on jsonb Data Type (Part 3/3).</a></strong></p>
<p>In this post, I am also going share one of the important query to convert PostgreSQL tabular data into JSON formatted data.</p>
<p>Generally, We are storing JSON formatted data into PostgreSQL and access base on the different filters.</p>
<blockquote><p>Here, I am sharing one type of utility script to convert PostgreSQL table data into JSON formatted data. Sometimes it requires to populate JSON formatted data for a web service purpose.</p></blockquote>
<p>I am doing this using, json_agg and row_to_json function.<br />
<strong>json_agg(expression) :</strong> aggregates values as a JSON array.<br />
<strong>row_to_json(record [, pretty_bool]) :</strong> Returns the row as JSON.</p>
<p><strong>Below is a full demonstration of this:</strong></p>
<p><strong>First, create table with sample data:</strong></p><pre class="crayon-plain-tag">CREATE TABLE tbl_EmployeeDetails
(
	EmpID INTEGER
	,DepartmenetName VARCHAR(50)
	,EmpFirstName VARCHAR(50)
	,EmpLastName VARCHAR(50)
);

INSERT INTO tbl_EmployeeDetails VALUES
(1,'Sales','Anvesh','Patel')
,(2,'Sales','Neevan','Patel')
,(3,'Order','Roy','Loother')
,(4,'Marketing','Martin','Farook')
,(5,'Marketing','Jenny','Pandya')
,(6,'Marketing','Mahi','Patel');</pre><p>
<strong>Script to convert data into JSON format without column name for key:</strong></p><pre class="crayon-plain-tag">SELECT 
	DepartmenetName
	,json_agg(row_to_json((EmpFirstName, EmpLastName))) AS JsonData
FROM tbl_EmployeeDetails 
GROUP BY DepartmenetName;</pre><p>
<strong>The Result:</strong></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2016/02/PostgreSQLJsonData1.png" rel="attachment wp-att-1815"><img fetchpriority="high" decoding="async" class="alignnone wp-image-1815 size-full" title="PostgreSQL Json Data" src="https://www.dbrnd.com/wp-content/uploads/2016/02/PostgreSQLJsonData1.png" alt="PostgreSQL Json Data" width="773" height="100" srcset="https://www.dbrnd.com/wp-content/uploads/2016/02/PostgreSQLJsonData1.png 773w, https://www.dbrnd.com/wp-content/uploads/2016/02/PostgreSQLJsonData1-300x39.png 300w, https://www.dbrnd.com/wp-content/uploads/2016/02/PostgreSQLJsonData1-750x97.png 750w, https://www.dbrnd.com/wp-content/uploads/2016/02/PostgreSQLJsonData1-150x19.png 150w" sizes="(max-width: 773px) 100vw, 773px" /></a></p>
<p><strong>Script to convert data into JSON format with column name for key:</strong></p><pre class="crayon-plain-tag">SELECT 
	DepartmenetName
	,json_agg(row_to_json
	(
	  (SELECT ColumnName FROM (SELECT EmpFirstName, EmpLastName) AS ColumnName (EmpFirstName, EmpLastName))
	)) AS JsonData
FROM tbl_EmployeeDetails
GROUP BY DepartmenetName;</pre><p>
<strong>The Result:</strong></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2016/02/PostgreSQLJsonData2.png" rel="attachment wp-att-1816"><img decoding="async" class="alignnone wp-image-1816 size-full" title="PostgreSQL Json Data" src="https://www.dbrnd.com/wp-content/uploads/2016/02/PostgreSQLJsonData2.png" alt="PostgreSQL Json Data" width="1170" height="98" srcset="https://www.dbrnd.com/wp-content/uploads/2016/02/PostgreSQLJsonData2.png 1170w, https://www.dbrnd.com/wp-content/uploads/2016/02/PostgreSQLJsonData2-300x25.png 300w, https://www.dbrnd.com/wp-content/uploads/2016/02/PostgreSQLJsonData2-768x64.png 768w, https://www.dbrnd.com/wp-content/uploads/2016/02/PostgreSQLJsonData2-1024x86.png 1024w, https://www.dbrnd.com/wp-content/uploads/2016/02/PostgreSQLJsonData2-750x63.png 750w, https://www.dbrnd.com/wp-content/uploads/2016/02/PostgreSQLJsonData2-150x13.png 150w" sizes="(max-width: 1170px) 100vw, 1170px" /></a></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>PostgreSQL: Using json_agg() aggregate table data into a JSON formatted array</title>
		<link>https://www.dbrnd.com/2016/03/postgresql-using-json_agg-aggregate-table-data-into-a-json-formatted-array/</link>
					<comments>https://www.dbrnd.com/2016/03/postgresql-using-json_agg-aggregate-table-data-into-a-json-formatted-array/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Fri, 25 Mar 2016 18:08:58 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[json_agg]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL 9.4]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=2173</guid>

					<description><![CDATA[Recently, PostgreSQL introduced JSON data type which is very powerful to manipulate JSON formatted data. I published a couple of articles on JSON Data Type. You can start from here. In this post, I am showing aggregation of table data into a JSON formatted array using json_agg(). Our requirement is to aggregate non json formatted [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-using-json_agg-aggregate-table-data-into-a-json-formatted-array%2F&amp;linkname=PostgreSQL%3A%20Using%20json_agg%28%29%20aggregate%20table%20data%20into%20a%20JSON%20formatted%20array" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-using-json_agg-aggregate-table-data-into-a-json-formatted-array%2F&amp;linkname=PostgreSQL%3A%20Using%20json_agg%28%29%20aggregate%20table%20data%20into%20a%20JSON%20formatted%20array" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-using-json_agg-aggregate-table-data-into-a-json-formatted-array%2F&amp;linkname=PostgreSQL%3A%20Using%20json_agg%28%29%20aggregate%20table%20data%20into%20a%20JSON%20formatted%20array" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-using-json_agg-aggregate-table-data-into-a-json-formatted-array%2F&amp;linkname=PostgreSQL%3A%20Using%20json_agg%28%29%20aggregate%20table%20data%20into%20a%20JSON%20formatted%20array" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-using-json_agg-aggregate-table-data-into-a-json-formatted-array%2F&amp;linkname=PostgreSQL%3A%20Using%20json_agg%28%29%20aggregate%20table%20data%20into%20a%20JSON%20formatted%20array" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-using-json_agg-aggregate-table-data-into-a-json-formatted-array%2F&amp;linkname=PostgreSQL%3A%20Using%20json_agg%28%29%20aggregate%20table%20data%20into%20a%20JSON%20formatted%20array" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2016%2F03%2Fpostgresql-using-json_agg-aggregate-table-data-into-a-json-formatted-array%2F&#038;title=PostgreSQL%3A%20Using%20json_agg%28%29%20aggregate%20table%20data%20into%20a%20JSON%20formatted%20array" data-a2a-url="https://www.dbrnd.com/2016/03/postgresql-using-json_agg-aggregate-table-data-into-a-json-formatted-array/" data-a2a-title="PostgreSQL: Using json_agg() aggregate table data into a JSON formatted array"></a></p><p>Recently, PostgreSQL introduced JSON data type which is very powerful to manipulate JSON formatted data. </p>
<p>I published a couple of articles on JSON Data Type.<br />
<a href="https://www.dbrnd.com/2016/02/postgresql-9-4-the-json-data-type-is-awesome-part-1/" target="_blank"><strong>You can start from here. </strong></a></p>
<p>In this post, I am showing aggregation of table data into a JSON formatted array using json_agg().</p>
<p>Our requirement is to aggregate non json formatted data into JSON array and even also required to aggregate JSON formatted data.</p>
<p>I prepared a small demonstration using json_agg().</p>
<p><strong>Create a table with sample JSON formatted data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_TestJSON
(
	ID INTEGER PRIMARY KEY
	,DepartName VARCHAR(250)
	,EmployeeDetails JSONB
);</pre> </p>
<pre class="crayon-plain-tag">INSERT INTO tbl_TestJSON
VALUES 
(
	1
	,'Sales'
	,'
	{"employees":[
	    {"firstName":"Anvesh", "lastName":"Patel"}	  
	]}'
)
,(
	2
	,'Production'
	,'
	{"employees":[	    
	    {"firstName":"Nancy", "lastName":"Shah"}
	]}'
);</pre> </p>
<p><strong>Aggregates JSON formatted data:</strong></p>
<pre class="crayon-plain-tag">SELECT json_agg(employeedetails) AS JsonArray FROM tbl_TestJSON;

JsonArray
------------------------
[{"employees": [{"lastName": "Patel", "firstName": "Anvesh"}]}, {"employees": [{"lastName": "Shah", "firstName": "Nancy"}]}]</pre> </p>
<p><strong>Aggregates non JSON formatted data:</strong></p>
<pre class="crayon-plain-tag">SELECT json_agg(DepartName) AS JsonArray FROM tbl_TestJSON;

JsonArray
------------------------
["Sales", "Production"]</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/03/postgresql-using-json_agg-aggregate-table-data-into-a-json-formatted-array/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
