Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pretty-print JSON script in MVC 4 API

How can I get the following JSON response to look cleaner using MVC 4 API. This is a sample JSON

{"Name":"Apple","Expiry":"2008-12-28T00:00:00","Price":3.99}

Pretty JSON

{
  "Name":"Apple",
  "Expiry":"2008-12-28T00:00:00",
  "Price":3.99
}
like image 564
Victor J. Garcia Avatar asked Oct 21 '12 15:10

Victor J. Garcia


2 Answers

You can do this using Json.net NuGet package:

JObject.Parse(json).ToString(Formatting.Indented)
like image 33
SHSE Avatar answered Sep 17 '22 07:09

SHSE


If you're using the Web Api. You can set:

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
like image 104
Lee Gunn Avatar answered Sep 18 '22 07:09

Lee Gunn