Saturday, January 23, 2010

JSON to XML and XML to JSON Converter in C#

What is JSON?

JSON, short for JavaScript Object Notation, is a lightweight computer data interchange format. It is a text-based, human-readable format for representing simple data structures and associative arrays (called objects). The JSON format was originally specified in RFC 4627 by Douglas Crockford. The official Internet media type for JSON is application/json. The JSON filename extension is .json. More information on JSON can be found at http://www.json.org

Some examples of XML and its equivalent JSON as given below:
XML: <xx yy='nn'><mm>zzz</mm></xx>
JSON: { "xx": {"yy":"nn","mm":"zzz"}}

XML: <xx yy='nn'><mm>zzz</mm><mm>aaa</mm></xx>
JSON: { "xx": {"yy":"nn", "mm":["zzz","aaa" ] } }

XML: <xx><mm>zzz</mm>some text</xx>
JSON: { "xx": {"mm":"zzz", "value":"some text"} }

XML: <xx value='yyy'>some text<mm>zzz</mm>more text</xx>
JSON: { "xx": {"mm":"zzz", "value":[ "yyy", "some text", "more text" ] } }

JSON to XML and XML to JSON Converter
James Newton-King has created an excellent library for JSON conversion in C#. The library has following features:
  • LINQ to JSON
  • The JsonSerializer for quickly converting your .NET objects to JSON and back again
  • Json.NET can optionally produce well formatted, indented JSON for debugging or display
  • Attributes like JsonIgnore and JsonProperty can be added to a class to customize how a class is serialized
  • Ability to convert JSON to and from XML
  • Supports multiple platforms: .NET, Silverlight and the Compact Framework

You can download the JSON C# library here. More information about the JSON Utilities library can be found at http://james.newtonking.com/projects/json-net.aspx.
Hats off James, this is an excellent work!

4 comments:

Anonymous said...

does it iterate through nodes in xml to convert to json or is it more like serialize/deserialize?

Silver MLM said...

Need to convert a file in .json to .xls, .cvs or .txt format any converters or hints?

Anonymous said...

Does anyone know if you can pass a JSON file to this?

AJ said...

What will happen if your project restricted for using the third party tools like NewtonSoft.Json, than How will you convert Json to xml ?
same issue with my organization is restricted to use the NewtonSoft.Net for converting Json to xml. actually my product based company dont want to dependent on third party tools.
so please help me...
Thank you so much in advance.

Post a Comment