Search code examples
c#jsonformatting

JSON formatter in C#?


Looking for a function that will take a string of Json as input and format it with line breaks and indentations. Validation would be a bonus, but isn't necessary, and I don't need to parse it into an object or anything.

Anyone know of such a library?


Sample input:

{"status":"OK", "results":[ {"types":[ "locality", "political"], "formatted_address":"New York, NY, USA", "address_components":[ {"long_name":"New York", "short_name":"New York", "types":[ "locality", "political"]}, {"long_name":"New York", "short_name":"New York", "types":[ "administrative_area_level_2", "political"]}, {"long_name":"New York", "short_name":"NY", "types":[ "administrative_area_level_1", "political"]}, {"long_name":"United States", "short_name":"US", "types":[ "country", "political"]}], "geometry":{"location":{"lat":40.7143528, "lng":-74.0059731}, "location_type":"APPROXIMATE", "viewport":{"southwest":{"lat":40.5788964, "lng":-74.2620919}, "northeast":{"lat":40.8495342, "lng":-73.7498543}}, "bounds":{"southwest":{"lat":40.4773990, "lng":-74.2590900}, "northeast":{"lat":40.9175770, "lng":-73.7002720}}}}]} 

Solution

  • This worked for me using System.Text.Json in .Net Core 3.1

     public string PrettyJson(string unPrettyJson)
     {
         var options = new JsonSerializerOptions(){
             WriteIndented = true
         };
    
         var jsonElement = JsonSerializer.Deserialize<JsonElement>(unPrettyJson);
    
         return JsonSerializer.Serialize(jsonElement, options);
     }