<?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>Diogo Guiomar, Author at Blog IT</title>
	<atom:link href="https://blogit.create.pt/author/diogoguiomar/feed/" rel="self" type="application/rss+xml" />
	<link>https://blogit.create.pt/author/diogoguiomar/</link>
	<description>Create IT blogger community</description>
	<lastBuildDate>Thu, 10 Jan 2019 12:46:09 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>Query a JSON array in SQL</title>
		<link>https://blogit.create.pt/diogoguiomar/2018/02/26/query-a-json-array-column-in-sql/</link>
					<comments>https://blogit.create.pt/diogoguiomar/2018/02/26/query-a-json-array-column-in-sql/#comments</comments>
		
		<dc:creator><![CDATA[Diogo Guiomar]]></dc:creator>
		<pubDate>Mon, 26 Feb 2018 11:07:45 +0000</pubDate>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Sql]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[openjson]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">http://blogit.create.pt/diogoguiomar/?p=44</guid>

					<description><![CDATA[<p>For the purpose of this post, lets not evaluate the db design option and lets focus on the operations on the json column. Lets say we have a table of customers where we have an Id, a CompanyName and an Address, although these customers can have some information related to a service, for example they [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/diogoguiomar/2018/02/26/query-a-json-array-column-in-sql/">Query a JSON array in SQL</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>For the purpose of this post, lets not evaluate the db design option and lets focus on the operations on the json column.</p>
<p>Lets say we have a table of customers where we have an Id, a CompanyName and an Address, although these customers can have some information related to a service, for example they may have a specific customer id for each service.</p>
<pre>Customer

Id: the id

CompanyName: the company name

Address: the address

--- ServiceId: 1

--- ServiceCustomerId: the customer id for service 1

--- ServiceId: 2

--- ServiceCustomerId: the customer id for service 2</pre>
<p>One way we could store this on a single table could be with these columns: Id / Name / Address / ServicesDataInJson</p>
<p>So for this example, the information on the ServicesDataInJson is an array of &#8216;objects&#8217; that contains the information of our customer on each service. Since there is no native JSON format on SQL Server, the data type of this column is just a nvarchar(max)</p>
<p>Here&#8217;s how a simple SELECT looks like:</p>
<p><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-74" src="http://blogit-create.com/wp-content/uploads/2018/02/image.png" alt="" width="1044" height="60" srcset="https://blogit.create.pt/wp-content/uploads/2018/02/image.png 1044w, https://blogit.create.pt/wp-content/uploads/2018/02/image-300x17.png 300w, https://blogit.create.pt/wp-content/uploads/2018/02/image-768x44.png 768w, https://blogit.create.pt/wp-content/uploads/2018/02/image-1024x59.png 1024w, https://blogit.create.pt/wp-content/uploads/2018/02/image-696x40.png 696w" sizes="(max-width: 1044px) 100vw, 1044px" /></p>
<p>Now we want to query our customers table by a customer service id (which is inside that json). How can we query this?</p>
<pre class="brush: sql; title: ; notranslate">

SELECT *

FROM Customers c

CROSS APPLY OPENJSON(c.ServicesDataInJson)

WITH (ServiceId int '$.ServiceId',

ServiceCustomerId nvarchar(255) '$.ServiceCustomerId') as jsonValues

WHERE jsonValues.ServiceCustomerId = @TheIdWeWantToSearchFor

</pre>
<p><strong>The OPENJSON is a table-valued function that parses the json into a row/column result and the WITH clause let us define how we want that output.</strong></p>
<p>Here is the query <strong>without</strong> the WHERE clause:</p>
<p><img decoding="async" class="alignnone size-full wp-image-114" src="http://blogit-create.com/wp-content/uploads/2018/02/image-1.png" alt="" width="970" height="82" srcset="https://blogit.create.pt/wp-content/uploads/2018/02/image-1.png 970w, https://blogit.create.pt/wp-content/uploads/2018/02/image-1-300x25.png 300w, https://blogit.create.pt/wp-content/uploads/2018/02/image-1-768x65.png 768w, https://blogit.create.pt/wp-content/uploads/2018/02/image-1-696x59.png 696w" sizes="(max-width: 970px) 100vw, 970px" /></p>
<p>Note: The OPENJSON function will not work if the database compatibility level is lower than 130.</p>
<p>To change it:</p>
<pre class="brush: sql; title: ; notranslate">ALTER DATABASE DatabaseName SET COMPATIBILITY_LEVEL = 130</pre>
<p>The post <a href="https://blogit.create.pt/diogoguiomar/2018/02/26/query-a-json-array-column-in-sql/">Query a JSON array in SQL</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/diogoguiomar/2018/02/26/query-a-json-array-column-in-sql/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
