Hello guys,
Good afternoon.
In this post I will demonstrate how to read JSON strings and convert to a table and convert data from a table to JSON and XML using only T-SQL. This tip is very useful when you are using an earlier version of SQL Server 2016 and need to read Json strings.
If you are using SQL Server 2016 or higher, be aware that native JSON support already exists. To learn how this native support works, read my post. SQL Server 2016 - Using Native JSON Support (JSON_VALUE, JSON_QUERY, OPENJSON, FOR JSON, ISJSON, JSON_MODIFY).
What is JSON
JSON, an acronym for "JavaScript Object Notation", is a lightweight format for exchanging computational data. JSON's simplicity has resulted in its widespread use, especially as an alternative to XML in AJAX.
One of the claimed advantages of JSON over XML as a format for data exchange in this context is that it is much easier to write a JSON parser. In JavaScript itself, JSON can be parsed trivially using the eval () function. This was important for JSON acceptance within the AJAX community due to the presence of this JavaScript feature in all current web browsers.
In practice, arguments about the ease of development and performance of the parser are rarely highlighted because of the security concerns of using eval () and the increasing integration of XML processing in modern web browsers. For this reason JSON is typically used in environments where the size of the data flow between client and server is of paramount importance (hence its use by Google, Yahoo, etc., which serve millions of users), where the source of data can be explicitly reliable, and where the loss of client-side XSLT processing resources for data manipulation or interface generation is not a consideration.
A great use for using JSON in SQL Server and what motivated me to make this post is for integrating and consuming data from webservices that use JSON to communicate with the information consumer client.
Reading a JSON string
Using the function that I'm going to post below, created by Phil Factor, you can read JSON strings easily, as in the example below:
Implementing the function:
View source
Creating a JSON string from the bank
Using this function, you can create a JSON string from a table in a specific format. For this we need to create this kind of data before using our function.
Prerequisite - Creating the function to handle the data:
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 33 34 |
IF (OBJECT_ID (N'dbo.fncJSON_Escape') IS NOT NULL) DROP FUNCTION dbo.fncJSON_Escape GO CREATE FUNCTION dbo.fncJSON_Escape ( @Ds_String NVARCHAR(MAX) ) RETURNS NVARCHAR(MAX) AS BEGIN SELECT @Ds_String = REPLACE(@Ds_String, FromString, ToString) FROM ( SELECT '\"' AS FromString, '"' AS ToString UNION ALL SELECT '\', '\\' UNION ALL SELECT '/', '\/' UNION ALL SELECT CHAR(08), '\b' UNION ALL SELECT CHAR(12), '\f' UNION ALL SELECT CHAR(10), '\n' UNION ALL SELECT CHAR(13), '\r' UNION ALL SELECT CHAR(09), '\t' ) substitutions RETURN @Ds_String END |
Prerequisite - Creating the JSON type:
1 2 3 4 5 6 7 8 9 10 11 |
IF EXISTS (SELECT * FROM sys.types WHERE name = 'JSON') DROP TYPE dbo.JSON CREATE TYPE dbo.JSON AS TABLE ( Id_Elemento INT NOT NULL, Nr_Sequencia [INT] NULL, Id_Objeto_Pai INT, Id_Objeto INT, Ds_Nome NVARCHAR(2000), Ds_String NVARCHAR(MAX) NOT NULL, Ds_Tipo VARCHAR(10) NOT NULL, PRIMARY KEY (Id_Elemento) ) |
Implementing the function:
View source
Creating an XML from a JSON
Using the function fncJSON_To_XML, you can create XML from a JSON string passed to the function, like the example below:
If you need help reading and manipulating data in XML objects using SQL Server, learn how to do this by reading my article. SQL Server - How to read, import, and export data from XML files.
Implementing the function:
View source
Workaround for converting a JSON string to XML
With the function below (written by Daniel Muchmacher), you can easily convert Json strings to an XML document, where you can use SQL Server to navigate between nodes and read the data as needed. This function is very useful for instances that are using an earlier version of SQL Server 2016 (without native JSON support), but you need to use this feature.
If you need help reading and manipulating data in XML objects using SQL Server, learn how to do this by reading my article. SQL Server - How to read, import, and export data from XML files.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
DECLARE @json VARCHAR(MAX) = '{ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": { "GlossEntry": { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": { "para": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": ["GML", "XML"] }, "GlossSee": "markup" } } } } }' SELECT dbo.fncJson2xml(@json) |
That's it.
Until the next post!
Are you blocked to copy the code?
I shouldn't .. I'll take a look
Very good the tip.