How to serialize a JObject without the formatting?

JsonSerializationjson.net

Json Problem Overview


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?

Json Solutions


Solution 1 - Json

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

Solution 2 - Json

You can also do the following;

string json = myJObject.ToString(Newtonsoft.Json.Formatting.None);

Solution 3 - Json

you can use JsonConvert.SerializeObject()

JsonConvert.SerializeObject(myObject) // myObject is returned by JObject.Parse() method

JsonConvert.SerializeObject()

JObject.Parse()

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHugoView Question on Stackoverflow
Solution 1 - JsonJames Newton-KingView Answer on Stackoverflow
Solution 2 - JsonSimpuView Answer on Stackoverflow
Solution 3 - JsonMawardyView Answer on Stackoverflow