Data Parsing SQL to JSON

Hello geeks, this article explains how to convert one type of data into another then again repeat the procedure for converting that type of data into another form. Basically data conversion, or I better say data parsing, sometimes takes all the focus of our project, so this article defines it all how to and why.

Outlines

  • Overview
  • Introduction
  • Requirements
  • Procedure
  • Summary

Overview

As I said above this article is all about data conversion, so I'll be explaining that in the entire article. I'll take some demo snippets to show you how it actually works and how easily you can do that using your SQL Server and simple SQL queries. I'll also demonstrate the requirement if necessary further in this article. So just brush up the entire article summary. I am showing a simple diagram to provide you a feel of what this entire article is all about.

Here's a summarized diagram:



Since this diagram says it all, first of all will use SQL Server tabular data then I'll convert into some XML data and further I'll use this XML data to convert it into JSON format (comma separated).

Introduction

So, now one of the most important questions is why we need this procedure and what the requirement is for converting simple SQL Server data into XML and JSON format.
The answer to this question is as simple as the procedure of converting the data itself. Thus, we will mention a few important facts and requirements for doing this procedure over our structured SQL Server data.

Requirements

  • In a data visualization procedure we need simple structured data for further operations, thus we require data in simple XML, JSON, CSV, TSV or some other format.
  • In creating charts, diagrams, data diagrams
  • In data manipulation
  • Storing data in simple structured format
  • For tool required data, since some tools accepts data in some pre-defined format for further operations
  • Data mining
  • Data warehousing

Procedure

I divided the entire procedure into two separate steps for data conversion. These two steps are as follows:

  • Tabular data
  • Tabular data into XML data
  • XML data into JSON data

Step 1: Tabular Data

First of all we need some data in our table, so for that I am creating a reference table named <Shopping>.

Reference Table

  1. -- Creating a table  
  2. CREATE TABLE Shopping  
  3. (  
  4.     Name NVARCHAR(50) not null,  
  5.     TDate DATETIME not null,  
  6.     Card_N0 INT not null,  
  7.     Country NVARCHAR(50) not null,  
  8.     Gender VARCHAR(10) not null,  
  9.     Age INT not null,  
  10.     TYear INT not null,  
  11.     TMonth INT not null,  
  12. );  
Inserting Data
  1. -- Inserting Values  
  2. INSERT INTO Shopping  
  3. VALUES('Doremon''2014-02-15', 987, 'USA''M', 32, 2014, 02);  
  4. INSERT INTO Shopping  
  5. VALUES('Dora''2014-02-05', 123, 'FRA''F', 26, 2014, 02);  
  6. INSERT INTO Shopping  
  7. VALUES('Popeye''2014-05-11', 487, 'IND''M', 32, 2014, 05);  
  8. INSERT INTO Shopping  
  9. VALUES('Minnie''2014-06-27', 436, 'UK''F', 25, 2014, 06);  
  10. INSERT INTO Shopping  
  11. VALUES('July''2014-09-16', 156, 'PR''F', 25, 2014, 09);  
  12. INSERT INTO Shopping  
  13. VALUES('Donald''2014-12-19', 907, 'JP''M', 32, 2014, 12);  
  14. INSERT INTO Shopping  
  15. VALUES('Goofy''2014-12-11', 023, 'AUS''M', 26, 2014, 12);  
Here's our demo table:

Tabular Data

Step 2: Tabular data into XML

Now will convert our tabular data into XML format, for that you need to write the following simple query:
  1. -- Generating XML Data  
  2. SELECT * FROM Shopping   
  3. FOR XML path, root;  

This simple SQL query will generate XML data for you. When you execute this query you will get output something like:

XML Data

In this output window simply click on <root>.

 

Result in Executed View

This will redirect you to another window where XML data will be waiting for you.
The XML data will be something like:

  1. <root>  
  2.     <row>  
  3.         <Name>Doremon</Name>  
  4.         <TDate>2014-02-15T00:00:00</TDate>  
  5.         <Card_N0>987</Card_N0>  
  6.         <Country>USA</Country>  
  7.         <Gender>M</Gender>  
  8.         <Age>32</Age>  
  9.         <TYear>2014</TYear>  
  10.         <TMonth>2</TMonth>  
  11.     </row>  
  12.     <row>  
  13.         <Name>Dora</Name>  
  14.         <TDate>2014-02-05T00:00:00</TDate>  
  15.         <Card_N0>123</Card_N0>  
  16.         <Country>FRA</Country>  
  17.         <Gender>F</Gender>  
  18.         <Age>26</Age>  
  19.         <TYear>2014</TYear>  
  20.         <TMonth>2</TMonth>  
  21.     </row>  
  22.     <row>  
  23.         <Name>Popeye</Name>  
  24.         <TDate>2014-05-11T00:00:00</TDate>  
  25.         <Card_N0>487</Card_N0>  
  26.         <Country>IND</Country>  
  27.         <Gender>M</Gender>  
  28.         <Age>32</Age>  
  29.         <TYear>2014</TYear>  
  30.         <TMonth>5</TMonth>  
  31.     </row>  
  32.     <row>  
  33.         <Name>Minnie</Name>  
  34.         <TDate>2014-06-27T00:00:00</TDate>  
  35.         <Card_N0>436</Card_N0>  
  36.         <Country>UK</Country>  
  37.         <Gender>F</Gender>  
  38.         <Age>25</Age>  
  39.         <TYear>2014</TYear>  
  40.         <TMonth>6</TMonth>  
  41.     </row>  
  42.     <row>  
  43.         <Name>July</Name>  
  44.         <TDate>2014-09-16T00:00:00</TDate>  
  45.         <Card_N0>156</Card_N0>  
  46.         <Country>PR</Country>  
  47.         <Gender>F</Gender>  
  48.         <Age>25</Age>  
  49.         <TYear>2014</TYear>  
  50.         <TMonth>9</TMonth>  
  51.     </row>  
  52.     <row>  
  53.         <Name>Donald</Name>  
  54.         <TDate>2014-12-19T00:00:00</TDate>  
  55.         <Card_N0>907</Card_N0>  
  56.         <Country>JP</Country>  
  57.         <Gender>M</Gender>  
  58.         <Age>32</Age>  
  59.         <TYear>2014</TYear>  
  60.         <TMonth>12</TMonth>  
  61.     </row>  
  62.     <row>  
  63.         <Name>Goofy</Name>  
  64.         <TDate>2014-12-11T00:00:00</TDate>  
  65.         <Card_N0>23</Card_N0>  
  66.         <Country>AUS</Country>  
  67.         <Gender>M</Gender>  
  68.         <Age>26</Age>  
  69.         <TYear>2014</TYear>  
  70.         <TMonth>12</TMonth>  
  71.     </row>  
  72. </root>  

 

Now we are half done. Now our next task is to converitn this XML data into JSON format. So let's go.

Step 3: XML data into JSON data

For converting XML data into JSON there are two sub-steps, they are:

XML to JSON Data

  • Declaration & Binding

    This is the first sub-step, for this step we only need to write a simple query for the declaration and bind XML data into it. For that we use some set of declare and set statement as:
    1. DECLARE @Shopping xml;  
    2. SET @Shopping =  
    3. '<?xml version="1.0" encoding="UTF-8"?>  
    4. <DATA goes here>’;  
    Here's a simple demonstration:
    1. -- Declaration & Binding  
    2. DECLARE @Shopping xml;  
    3. SET @Shopping =  
    4. '<?xml version="1.0" encoding="UTF-8"?>  
    5. <root>  
    6.     <row>  
    7.         <Name>Doremon</Name>  
    8.         <TDate>2014-02-15T00:00:00</TDate>  
    9.         <Card_N0>987</Card_N0>  
    10.         <Country>USA</Country>  
    11.         <Gender>M</Gender>  
    12.         <Age>32</Age>  
    13.         <TYear>2014</TYear>  
    14.         <TMonth>2</TMonth>  
    15.     </row>  
    16.     <row>  
    17.         <Name>Dora</Name>  
    18.         <TDate>2014-02-05T00:00:00</TDate>  
    19.         <Card_N0>123</Card_N0>  
    20.         <Country>FRA</Country>  
    21.         <Gender>F</Gender>  
    22.         <Age>26</Age>  
    23.         <TYear>2014</TYear>  
    24.         <TMonth>2</TMonth>  
    25.     </row>  
    26.     <row>  
    27.         <Name>Popeye</Name>  
    28.         <TDate>2014-05-11T00:00:00</TDate>  
    29.         <Card_N0>487</Card_N0>  
    30.         <Country>IND</Country>  
    31.         <Gender>M</Gender>  
    32.         <Age>32</Age>  
    33.         <TYear>2014</TYear>  
    34.         <TMonth>5</TMonth>  
    35.     </row>  
    36.     <row>  
    37.         <Name>Minnie</Name>  
    38.         <TDate>2014-06-27T00:00:00</TDate>  
    39.         <Card_N0>436</Card_N0>  
    40.         <Country>UK</Country>  
    41.         <Gender>F</Gender>  
    42.         <Age>25</Age>  
    43.         <TYear>2014</TYear>  
    44.         <TMonth>6</TMonth>  
    45.     </row>  
    46.     <row>  
    47.         <Name>July</Name>  
    48.         <TDate>2014-09-16T00:00:00</TDate>  
    49.         <Card_N0>156</Card_N0>  
    50.         <Country>PR</Country>  
    51.         <Gender>F</Gender>  
    52.         <Age>25</Age>  
    53.         <TYear>2014</TYear>  
    54.         <TMonth>9</TMonth>  
    55.     </row>  
    56.     <row>  
    57.         <Name>Donald</Name>  
    58.         <TDate>2014-12-19T00:00:00</TDate>  
    59.         <Card_N0>907</Card_N0>  
    60.         <Country>JP</Country>  
    61.         <Gender>M</Gender>  
    62.         <Age>32</Age>  
    63.         <TYear>2014</TYear>  
    64.         <TMonth>12</TMonth>  
    65.     </row>  
    66.     <row>  
    67.         <Name>Goofy</Name>  
    68.         <TDate>2014-12-11T00:00:00</TDate>  
    69.         <Card_N0>23</Card_N0>  
    70.         <Country>AUS</Country>  
    71.         <Gender>M</Gender>  
    72.         <Age>26</Age>  
    73.         <TYear>2014</TYear>  
    74.         <TMonth>12</TMonth>  
    75.     </row>  
    76. </root>';  
  • Data Conversion

    For data conversion we will use two simple functions, both for different functionality, these functions are:

    • STUFF

      The STUFF function inserts a string into another string. It deletes a specified length of characters in the first string at the start position and then inserts the second string into the first string at the start position.
       
    • COALESCE

      For structured comma separated data:
    1. -- Function for Conversion | XML to JSON  
    2. SELECT Stuff(   
    3.     (SELECT * from   
    4.         (SELECT ',  
    5.         {'+   
    6.             Stuff((SELECT ',"'+coalesce(b.c.value('local-name(.)''NVARCHAR(MAX)'),'')+'":"'+  
    7.             b.c.value('text()[1]','NVARCHAR(MAX)') +'"'  
    8.   
    9.             from x.a.nodes('*') b(c)   
    10.             for xml path(''),TYPE).value('(./text())[1]','NVARCHAR(MAX)'),1,1,'')+  
    11.         '}'   
    12.         from @Shopping.nodes('/root/*') x(a)   
    13.     ) JSON(theLine)   
    14. for xml path(''),TYPE).value('.','NVARCHAR(MAX)' ) ,1,1,'')  
    That operation will give you this in the output window:

    JSON Data

    This is nothing but your JSON data. If you copy and paste it in another window, it will be something like this:

    {"Name":"Doremon","TDate":"2014-02-15T00:00:00","Card_N0":"987","Country":"USA","Gender":"M","Age":"32","TYear":"2014","TMonth":"2"},

    {"Name":"Dora","TDate":"2014-02-05T00:00:00","Card_N0":"123","Country":"FRA","Gender":"F","Age":"26","TYear":"2014","TMonth":"2"},

    {"Name":"Popeye","TDate":"2014-05-11T00:00:00","Card_N0":"487","Country":"IND","Gender":"M","Age":"32","TYear":"2014","TMonth":"5"},

    {"Name":"Minnie","TDate":"2014-06-27T00:00:00","Card_N0":"436","Country":"UK","Gender":"F","Age":"25","TYear":"2014","TMonth":"6"},

    {"Name":"July","TDate":"2014-09-16T00:00:00","Card_N0":"156","Country":"PR","Gender":"F","Age":"25","TYear":"2014","TMonth":"9"},

    {"Name":"Donald","TDate":"2014-12-19T00:00:00","Card_N0":"907","Country":"JP","Gender":"M","Age":"32","TYear":"2014","TMonth":"12"},

    {"Name":"Goofy","TDate":"2014-12-11T00:00:00","Card_N0":"23","Country":"AUS","Gender":"M","Age":"26","TYear":"2014","TMonth":"12"}

Now, it is done.

Summary

This was a simple demonstration of data conversion from one to another form. You can do these same operations by writing lines of codes or using JSON serialization. In spite of all these you can also generate some other type of data too.

I hope you will like it and if you experience any problem in this operation then feel free to ping or message me anytime. I would love to answer your queries.

Up Next
    Ebook Download
    View all
    Learn
    View all