how to format Json output? [duplicate] how to format Json output? [duplicate] json json

how to format Json output? [duplicate]


I wouldn't change the format written out by the web service, but if you want to format it for diagnostic purposes you can use Json.NET to do this very simply:

JObject json = JObject.Parse(text);string formatted = json.ToString();

The result is automatically formatted. You could put this into a small tool - either a desktop tool or a web page somewhere. (I wouldn't be surprised if there were already online JSON formatters, although obviously you'd want to be careful about formatting sensitive data.)


Jon's answer doesn't seem to work if the root element of your json is an array. Using JToken instead of JObject fixed this for me. As an extension method on string, this looks like:

public static string FormatJson(this string json){    return JToken.Parse(json).ToString();}


If you call your service from Firefox there's this nice plugin that will prettify the JSON for you: JSONView

I also used to use this website to format and validate any JSON: JSON Formatter