How to serialize a JObject without the formatting?

Hugo picture Hugo · Feb 20, 2009 · Viewed 113.5k times · Source

I have a JObject (I'm using Json.Net) that I constructed with LINQ to JSON (also provided by the same library). When I call the ToString() method on the JObject, it outputs the results as formatted JSON.

How do I set the formatting to "none" for this?

Answer

James Newton-King picture James Newton-King · Feb 21, 2009

Call JObject's ToString(Formatting.None) method.

Alternatively if you pass the object to the JsonConvert.SerializeObject method it will return the JSON without formatting.

Documentation: Write JSON text with JToken.ToString