Json_To_Xml
Converts a JSON string to XML
- Free Function
- SQL Server Compatibility: 2008, 2012, 2014, 2016, 2017
SELECT SQLHTTP.net.Json_To_Xml ( @Json, @NodeStyle )
Name | Type | Description |
---|---|---|
@Json | nvarchar(MAX) | Json string |
@NodeStyle | tinyint |
One of these three values. See examples below.
|
xml
The JsonDefaultNoNameNode field in the Defaults table can be updated to assign a different default node name for Json objects or arrays that are not named. Default value: NoName
The following JSON string is used for the examples below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
DECLARE @Json nvarchar(MAX) SET @Json = '{ "firstName": "John", "lastName": "Smith", "isAlive": true, "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100" }, "phoneNumbers": [ { "type": "home", "number": "212 555-1234" }, { "type": "office", "number": "646 555-4567" }, { "type": "mobile", "number": "123 456-7890" } ], "children": [], "spouse": null }' |
Using @NodeStyle = 0:
1 2 3 |
SELECT SQLHTTP.net.Json_To_Xml(@Json, 0) |
1 2 3 4 5 6 7 8 9 10 11 |
<JsonObject age="25" firstName="John" isAlive="true" lastName="Smith"> <address city="New York" postalCode="10021-3100" state="NY" streetAddress="21 2nd Street" /> <children /> <phoneNumbers> <JsonObject number="212 555-1234" type="home" /> <JsonObject number="646 555-4567" type="office" /> <JsonObject number="123 456-7890" type="mobile" /> </phoneNumbers> </JsonObject> |
Using @NodeStyle = 1:
1 2 3 |
SELECT SQLHTTP.net.Json_To_Xml(@Json, 1) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<NoName JsonType="Object"> <address JsonType="Object"> <city JsonType="String">New York</city> <postalCode JsonType="String">10021-3100</postalCode> <state JsonType="String">NY</state> <streetAddress JsonType="String">21 2nd Street</streetAddress> </address> <age JsonType="Number">25</age> <children JsonType="Array" /> <firstName JsonType="String">John</firstName> <isAlive JsonType="Boolean">true</isAlive> <lastName JsonType="String">Smith</lastName> <phoneNumbers JsonType="Array"> <NoName JsonType="Object"> <number JsonType="String">212 555-1234</number> <type JsonType="String">home</type> </NoName> <NoName JsonType="Object"> <number JsonType="String">646 555-4567</number> <type JsonType="String">office</type> </NoName> <NoName JsonType="Object"> <number JsonType="String">123 456-7890</number> <type JsonType="String">mobile</type> </NoName> </phoneNumbers> <spouse JsonType="Null" /> </NoName> |
Using @NodeStyle = 2:
1 2 3 |
SELECT SQLHTTP.net.Json_To_Xml(@Json, 2) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<JsonObject> <JsonObject Name="address"> <JsonString Name="city">New York</JsonString> <JsonString Name="postalCode">10021-3100</JsonString> <JsonString Name="state">NY</JsonString> <JsonString Name="streetAddress">21 2nd Street</JsonString> </JsonObject> <JsonNumber Name="age">25</JsonNumber> <JsonArray Name="children" /> <JsonString Name="firstName">John</JsonString> <JsonBoolean Name="isAlive">true</JsonBoolean> <JsonString Name="lastName">Smith</JsonString> <JsonArray Name="phoneNumbers"> <JsonObject> <JsonString Name="number">212 555-1234</JsonString> <JsonString Name="type">home</JsonString> </JsonObject> <JsonObject> <JsonString Name="number">646 555-4567</JsonString> <JsonString Name="type">office</JsonString> </JsonObject> <JsonObject> <JsonString Name="number">123 456-7890</JsonString> <JsonString Name="type">mobile</JsonString> </JsonObject> </JsonArray> <JsonNull Name="spouse" /> </JsonObject> |