How can I Dump() a Newtonsoft JObject in LinqPad?

My guess is that you're doing something like this:

dynamic foo = ...;
foo.Dump();

Extension methods (which Dump is) don't work with dynamic typing. If you use:

object foo = ...;
foo.Dump();

then I expect it will "work" fine. It may well not do what you actually want though - because the properties on JObject aren't the JSON properties, which are provided dynamically.

(Calling the extension method explicitly as per D Stanley's answer will work too, but you may find it more convenient to still do it as an extension method.)

EDIT: I strongly suspect that Dump simply won't give you what you want, given that it doesn't know anything about Json.NET and probably doesn't handle dynamic objects in the way that (say) the debugger would. You'd probably be best off writing your own Dump method to iterate over the properties of the JObject and dump them recursively. If you're still hoping to find something out of the box, you should probably look in the Json.NET documentation rather than looking to LINQPad, although I don't know whether you'll find anything.


It's a static extension method, so you can call it as a static method:

LINQPad.Extensions.Dump(jObject);

I see that happen on some types when (I presume) the compiler isn't able to bind to the extension for some reason.

There's a post on LinqPad 's site and a blog post regarding using Dump() with dynamic objects.

You might try creating another Dump() extension that examines the properties of JObject and creating a Dictionary that can be Dumped prettily.

Something like this: (complete WAG based on JObject's definition):

var values = jObject.Properties.ToDictionary(p=>p.Name, p=>p.Value);
values.Dump();

of course you could add recursion for nested objects, etc.:

//Usage: GetProperties(jObject).Dump();
public static object GetProperties(object o)
{
    JObject j = o as JObject;
    if(j == null)
    {
        return o.ToString();
    }
    return j.Properties().ToDictionary(p=>p.Name,p=>GetProperties(p.Value));
}

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.