In LinqPad, trying to call .Dump()
on a Newtonsoft JSON.Net JObject
yields an exception:
RuntimeBinderException: 'Newtonsoft.Json.Linq.JObject' does not contain a definition for 'Dump'.
This works for almost everything else in LinqPad. I'd like to figure out a method that will Dump out a Newtonsoft JObject
, just like other objects, showing property names, values, etc.
I've already figured out how to get it to dump the JSON string, but I'd like to see an object get output rather than just a text string.
For anyone who lands here wanting to get pretty LINQPad output from a JSON string, deserializing to ExpandoObject
is an effective approach and works recursively down any hierarchies that may be in the data:
JsonConvert.DeserializeObject<ExpandoObject>(myJSONString).Dump();
Extending that to cover the actual question, an extension method on JObject along these lines would do the trick:
public static class ExtMethods
{
public static JObject DumpPretty(this JObject jo)
{
var jsonString = JsonConvert.SerializeObject(jo);
JsonConvert.DeserializeObject<ExpandoObject>(jsonString).Dump();
return jo; // return input in the spirit of LINQPad's Dump() method.
}
}
Not the most efficient method, but for quick use when digging around in LINQPad it will do the trick.