Minify a Json String Using. Net

Minify a Json String Using. Net

How can an existing json string be cleaned up/minfied? I've seen regexes being used. Any other (maybe more efficient) approach?

1

1 Answer

Install-Package Newtonsoft.Json

Just parse it and then serialize back into JSON:

var jsonString = "  {  title: \"Non-minified JSON string\"  }  ";
var obj = JsonConvert.DeserializeObject(jsonString);
jsonString = JsonConvert.SerializeObject(obj);

SerializeObject(obj, Formatting.None) method accepts Formatting enum as a second parameter. You can always choose if you want Formatting.Indented or Formatting.None.

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.