Click here to Skip to main content
13,254,754 members (49,699 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


26 bookmarked
Posted 13 Apr 2011

Convert DataSet or XML to JSON using VB.NET

, 18 Apr 2011
Rate this:
Please Sign up or sign in to vote.
Convert DataSet or XML to JSON using VB.NET
I searched high and wide for something simple to do this and every function I found basically reinvented the wheel from scratch. If you do any amount of searching, you can find the JavaScriptSerializer object which will convert objects to JSON. Sounds easy enough...WRONG! Even converting an empty DataSet will return a circular reference exception.

On a similar note, I found several articles claiming to convert XML to JSON using the same function. The "solution" was something like this:

Dim jss As New JavaScriptSerializer
Dim jsonString As String = jss.Serialize(DataSet1.GetXML())

I tried this thinking it made sense. But instead of serializing the data in the XML, it simply dumps the entire XML string into a single element in the JSON string. I'm pretty sure that's not what anyone wants it to do.

So how do you do it? Well, there are a number of methods, but I found the quickest and easiest is to just convert your DataSet to a Dictionary object, then use the JavaScriptSerializer to make the conversion.

Here's the code:

Function DataSetToJSON(ds As DataSet) As String
    Dim dict As New Dictionary(Of String, Object)
    For Each dt As DataTable In ds.Tables
        Dim arr(dt.Rows.Count) As Object
        For i As Integer = 0 To dt.Rows.Count - 1
            arr(i) = dt.Rows(i).ItemArray
        dict.Add(dt.TableName, arr)
    Dim json As New JavaScriptSerializer
    Return json.Serialize(dict)
End Function

If you're looking to convert XML to JSON and write the least amount of code, you can just write it to a DataSet on the fly like this:

DataSetToJSON(New DataSet().ReadXml(xmlString))

I know it sounds crazy, but both of these methods are really simple and execute really fast. And you don't need to know about XLT or anything else. Oh, and the only libraries you need are System.Data and System.Web.Script.Serialization.

When it comes around to referencing the JSON object from your browser, you can access the elements like so:


If anyone knows how to make the conversion, you can reference it by jsonString.TableName.Column[rowIndex], I would love to see it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jesse Fatherree
Systems Engineer
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionEven simpler and cleaner solution than using those whacky dictionaries (VB.Net) Pin
Rick Levin14-Nov-14 12:30
memberRick Levin14-Nov-14 12:30 
QuestionNice and simple Pin
kevininstructor29-Jul-13 12:12
memberkevininstructor29-Jul-13 12:12 
QuestionNice solution but what about column names? Pin
morganrg10-Jan-13 3:28
membermorganrg10-Jan-13 3:28 
Is there any way to include column names in the generated json?
AnswerRe: Nice solution but what about column names? Pin
Member 938852423-Jan-13 6:47
memberMember 938852423-Jan-13 6:47 
GeneralRe: Nice solution but what about column names? Pin
Member 1047045916-Dec-13 11:40
memberMember 1047045916-Dec-13 11:40 
QuestionNice article but . . . Pin
wrlucas11-Oct-12 13:50
memberwrlucas11-Oct-12 13:50 
AnswerRe: Nice article but . . . Pin
Jesse Fatherree11-Oct-12 14:41
memberJesse Fatherree11-Oct-12 14:41 
GeneralRe: Nice article but . . . Pin
wrlucas12-Oct-12 7:12
memberwrlucas12-Oct-12 7:12 
GeneralRe: Nice article but . . . Pin
Jesse Fatherree12-Oct-12 8:20
memberJesse Fatherree12-Oct-12 8:20 
GeneralWhat if I want to convert Json to a dataset? Pin
mmakar5-Nov-11 18:32
membermmakar5-Nov-11 18:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 18 Apr 2011
Article Copyright 2011 by Jesse Fatherree
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid