<?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 &#8211; SQLpowered.com</title>
	<atom:link href="https://sqlpowered.com/script-category/json/feed/" rel="self" type="application/rss+xml" />
	<link>https://sqlpowered.com</link>
	<description>SQL Server + BI</description>
	<lastBuildDate>Sun, 01 Jan 2023 09:42:54 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://sqlpowered.com/wp-content/uploads/2020/07/FavIcon-e1594067873682-99x100.png</url>
	<title>JSON &#8211; SQLpowered.com</title>
	<link>https://sqlpowered.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Generate the WITH part for the OPENJSON() function</title>
		<link>https://sqlpowered.com/script/5516/</link>
		
		<dc:creator><![CDATA[Jan Dvořák]]></dc:creator>
		<pubDate>Sun, 01 Jan 2023 09:40:36 +0000</pubDate>
				<guid isPermaLink="false">https://sqlpowered.com/?post_type=script&#038;p=5516</guid>

					<description><![CDATA[This simple script helps you prepare the WITH statement part for the OPENJSON() function. For large JSON data it will scan all nodes and build a distinct list of all available elements. You can adjust it for your needs (different nesting level etc.). DECLARE @JSON NVARCHAR(MAX) SET @JSON = N'{...]]></description>
										<content:encoded><![CDATA[<p>This simple script helps you prepare the WITH statement part for the OPENJSON() function. For large JSON data it will scan all nodes and build a distinct list of all available elements. You can adjust it for your needs (different nesting level etc.).</p>
<pre class="EnlighterJSRAW" data-enlighter-language="sql">DECLARE @JSON NVARCHAR(MAX)

SET @JSON = 
N'{
	"Employees": [
		{
			"Id": 1,
			"FirstName": "John",
			"LastName": "Doe"
		},
		{
			"Id": 2,
			"FirstName": "Paula",
			"LastName": "Klein",
			"BornName": "Smith",
			"Address": "Cross road 123, NY",
			"Married": true,
			"Kids": ["Ema", "Nick"]
		}
	]
}'

SELECT  
	DISTINCT '[' + [g].[Key] + '] ' + CASE [g].[Type]
						WHEN 1 THEN 'NVARCHAR(4000)'
						WHEN 2 THEN 'INT'
						WHEN 3 THEN 'BIT'
						WHEN 4 THEN 'NVARCHAR(MAX) AS JSON'
						WHEN 5 THEN 'NVARCHAR(MAX) AS JSON'
						ELSE 'X'
					END + ' ''$.' + [g].[Key] + ''','
FROM OPENJSON(@JSON) [e]
	CROSS APPLY OPENJSON([e].[Value]) [f]
	CROSS APPLY OPENJSON([f].[Value]) [g]
ORDER BY 1

SELECT 
	[g].*
FROM OPENJSON(@JSON) [e]
	CROSS APPLY OPENJSON([e].[Value]) [f]
	CROSS APPLY OPENJSON([f].[Value])
	WITH (
		[Address]	NVARCHAR(4000)	'$.Address',
		[BornName]	NVARCHAR(4000)	'$.BornName',
		[FirstName]	NVARCHAR(4000)	'$.FirstName',
		[Id]		INT		'$.Id',
		[LastName]	NVARCHAR(4000)	'$.LastName',
		[Married]	BIT		'$.Married',
		[Kids]		NVARCHAR(MAX)	'$.Kids' AS JSON
	) [g]
GO</pre>
<p>&nbsp;</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
