001
001

Reputation: 65147

how to exclude null from JSON? (after converting from XML)

Below is code to convert xml to json using http://json.codeplex.com/

how to exclude null from JSON? (ie "SessionId": "null")

string xml = ""; //see XML value below
XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);

            string jsonText = JsonConvert.SerializeXmlNode(doc); //See Json value below

Xml Input

<MyResponse>

    <Timestamp>2012-01-07T12:43:29</Timestamp>

    <SessionId></SessionId>

</MyResponse>

Json Output

{"MyResponse":{"Timestamp":"2012-01-07T12:43:29","SessionId":null}}

Upvotes: 1

Views: 1453

Answers (2)

Raj doshi
Raj doshi

Reputation: 1

It is not giving null property like this. It gives like nil to true as attribute in xml element.

Upvotes: 0

th3n3wguy
th3n3wguy

Reputation: 3737

You could have a simple string replace since you are outputting the JSON as a string. Do something like this:

jsonText = jsonText.Replace("null", "\"\"");

That should replace every occurrence of null with "".

Upvotes: 3

Related Questions