<?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>SQL Json to XML Function - MSSQL Query</title>
	<atom:link href="https://mssqlquery.com/tag/sql-json-to-xml-function/feed" rel="self" type="application/rss+xml" />
	<link>https://mssqlquery.com</link>
	<description>MSSQL and TSQL Programming and TSQL Examples</description>
	<lastBuildDate>Wed, 22 Mar 2023 21:33:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.3.2</generator>

<image>
	<url>https://mssqlquery.com/wp-content/uploads/2023/06/cropped-mssql-query-icon-32x32.png</url>
	<title>SQL Json to XML Function - MSSQL Query</title>
	<link>https://mssqlquery.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Function That Converts JSON Data to XML in SQL Server</title>
		<link>https://mssqlquery.com/function-that-converts-json-data-to-xml-in-sql-server</link>
		
		<dc:creator><![CDATA[Yavuz Selim Kart]]></dc:creator>
		<pubDate>Tue, 26 Oct 2021 20:20:10 +0000</pubDate>
				<category><![CDATA[Functions]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[JSON Data to XML in SQL Server]]></category>
		<category><![CDATA[JSON Data to XML in SQL Server Function]]></category>
		<category><![CDATA[SQL Json to XML Function]]></category>
		<guid isPermaLink="false">https://mssqlquery.com/?p=236</guid>

					<description><![CDATA[<p>Hello to everyone, In this article, I will try to give information about the use of the function that converts JSON Data to XML in SQL Server. In SQL Server, in some cases you may need to convert your JSON data to XML. You can easily do this using the&#46;&#46;&#46;</p>
<p>The post <a href="https://mssqlquery.com/function-that-converts-json-data-to-xml-in-sql-server">Function That Converts JSON Data to XML in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Hello to everyone,</p>
<p>In this article, I will try to give information about the use of the function that converts JSON Data to XML in SQL Server.</p>
<p>In SQL Server, in some cases you may need to convert your JSON data to XML.</p>
<p>You can easily do this using the function below.</p>
<pre class="line-numbers"><code class="language-sql">--Fonksiyonun oluşturulması


CREATE FUNCTION dbo.JSONTOXML
(
    @json VARCHAR(MAX)
)
RETURNS XML
AS
BEGIN;
    DECLARE @output VARCHAR(MAX),
            @key VARCHAR(MAX),
            @value VARCHAR(MAX),
            @recursion_counter INT,
            @offset INT,
            @nested BIT,
            @array BIT,
            @tab CHAR(1) = CHAR(9),
            @cr CHAR(1) = CHAR(13),
            @lf CHAR(1) = CHAR(10);

    SET @json = LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(@json, @cr, ''), @lf, ''), @tab, '')));

    IF (LEFT(@json, 1) != '{' OR RIGHT(@json, 1) != '}')
        RETURN '';

    SET @json = LTRIM(RTRIM(SUBSTRING(@json, 2, LEN(@json) - 2)));

    SELECT @output = '';
    WHILE (@json != '')
    BEGIN;

        IF (LEFT(@json, 1) != '"')
            RETURN 'Expected quote (start of key name). Found "' + LEFT(@json, 1) + '"';

        SET @key = SUBSTRING(@json, 2, PATINDEX('%[^\\]"%', SUBSTRING(@json, 2, LEN(@json)) + ' "'));

        SET @json = LTRIM(SUBSTRING(@json, LEN(@key) + 3, LEN(@json)));

        IF (LEFT(@json, 1) != ':')
            RETURN 'Expected ":" after key name, found "' + LEFT(@json, 1) + '"!';

        SET @json = LTRIM(SUBSTRING(@json, 2, LEN(@json)));

        IF (LEFT(@json, 1) = '[')
            SELECT @array = 1,
                   @json = LTRIM(SUBSTRING(@json, 2, LEN(@json)));

        IF (@array IS NULL)
            SET @array = 0;
        WHILE (@array IS NOT NULL)
        BEGIN;

            SELECT @value = NULL,
                   @nested = 0;

            IF (@value IS NULL AND LEFT(@json, 1) = '{')
            BEGIN;
                SELECT @recursion_counter = 1,
                       @offset = 1;
                WHILE (@recursion_counter != 0 AND @offset &lt; LEN(@json))
                BEGIN;
                    SET @offset = @offset + PATINDEX('%[{}]%', SUBSTRING(@json, @offset + 1, LEN(@json)));
                    SET @recursion_counter = @recursion_counter + (CASE SUBSTRING(@json, @offset, 1)
                                                                       WHEN '{' THEN
                                                                           1
                                                                       WHEN '}' THEN
                                                                           -1
                                                                   END
                                                                  );
                END;

                SET @value = CAST(dbo.JSONTOXML(LEFT(@json, @offset)) AS VARCHAR(MAX));
                SET @json = SUBSTRING(@json, @offset + 1, LEN(@json));
                SET @nested = 1;
            END;

            IF (@value IS NULL AND LEFT(@json, 2) = '""')
                SELECT @value = '',
                       @json = LTRIM(SUBSTRING(@json, 3, LEN(@json)));

            IF (@value IS NULL AND LEFT(@json, 1) = '"')
            BEGIN;
                SET @value = SUBSTRING(@json, 2, PATINDEX('%[^\\]"%', SUBSTRING(@json, 2, LEN(@json)) + ' "'));
                SET @json = LTRIM(SUBSTRING(@json, LEN(@value) + 3, LEN(@json)));
            END;

            IF (@value IS NULL AND LEFT(@json, 1) = ',')
                SET @value = '';

            IF (@value IS NULL)
            BEGIN;
                SET @value = LEFT(@json, PATINDEX('%[,}]%', REPLACE(@json, ']', '}') + '}') - 1);
                SET @json = SUBSTRING(@json, LEN(@value) + 1, LEN(@json));
            END;

            SET @output
                = @output + @lf + @cr + REPLICATE(@tab, @@NESTLEVEL - 1) + '&lt;' + @key + '&gt;'
                  + ISNULL(REPLACE(REPLACE(@value, '\"', '"'), '\\', '\'), '')
                  + (CASE
                         WHEN @nested = 1 THEN
                             @lf + @cr + REPLICATE(@tab, @@NESTLEVEL - 1)
                         ELSE
                             ''
                     END
                    ) + '&lt;/' + @key + '&gt;';

            IF (@array = 0 AND @json != '' AND LEFT(@json, 1) != ',')
                RETURN @output + 'Expected "," after value, found "' + LEFT(@json, 1) + '"!';

            IF (@array = 1 AND LEFT(@json, 1) NOT IN ( ',', ']' ))
                RETURN @output + 'In array, expected "]" or "," after ' + 'value, found "' + LEFT(@json, 1) + '"!';

            IF (@array = 1 AND LEFT(@json, 1) = ']')
            BEGIN;
                SET @array = NULL;
                SET @json = LTRIM(SUBSTRING(@json, 2, LEN(@json)));

                IF (LEFT(@json, 1) NOT IN ( '', ',' ))
                BEGIN
                    RETURN 'Closed array, expected ","!';
                END;
            END;

            SET @json = LTRIM(SUBSTRING(@json, 2, LEN(@json) + 1));
            IF (@array = 0)
                SET @array = NULL;

        END;
    END;
    RETURN CAST(@output AS XML);
END;


--Fonksiyonun kullanımı

DECLARE @json varchar(MAX);

SET @json
    = '{
"Kisi": {
    "ad": "Yavuz Selim",
    "soyad": "Kart",
    "yas": [33,34,35],
    "Adres": {
        "adres":"Kayaşehir",
        "sehir" :"İstanbul",
        "postakodu":"65536"
    },
        "telefonlar": {
            "ev":"212 555-6699",
			"is":"212 666-7788"
        }
    }
}';

SELECT dbo.JSONTOXML(@json);</code></pre>
<p>When you create the above function and run the code, you will see a result similar to the one below.</p>
<p><img decoding="async" fetchpriority="high" class="alignnone wp-image-239 size-full" src="https://mssqlquery.com/wp-content/uploads/2021/10/function-that-converts-json-data-to-xml-in-sql-server-1.jpg" alt="Function That Converts JSON Data to XML in SQL Server" width="700" height="585" srcset="https://mssqlquery.com/wp-content/uploads/2021/10/function-that-converts-json-data-to-xml-in-sql-server-1.jpg 700w, https://mssqlquery.com/wp-content/uploads/2021/10/function-that-converts-json-data-to-xml-in-sql-server-1-300x251.jpg 300w" sizes="(max-width: 700px) 100vw, 700px" /></p>
<p>As you can see, JSON data has been converted to XML.</p>
<p>Good luck to everyone in business and life.</p>
<div class='epvc-post-count'><span class='epvc-eye'></span>  <span class="epvc-count"> 382</span><span class='epvc-label'> Views</span></div><p>The post <a href="https://mssqlquery.com/function-that-converts-json-data-to-xml-in-sql-server">Function That Converts JSON Data to XML in SQL Server</a> first appeared on <a href="https://mssqlquery.com">MSSQL Query</a>.</p>]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
