13

I want to create a JSON file for use as part of a simple web prototyping exercise. LinqPAD is perfect for accessing the data from my DB in just the shape I need, however I cannot get it out as JSON very easily.

I don't really care what the schema is, because I can adapt my JavaScript to work with whatever is returned.

Is this possible?

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742

4 Answers4

24

A more fluent solution is to add the following methods to the "My Extensions" File in Linqpad:

public static String DumpJson<T>(this T obj)
{
    return
        obj
        .ToJson()
        .Dump();
}

public static String ToJson<T>(this T obj)
{
    return
        new System.Web.Script.Serialization.JavaScriptSerializer()
        .Serialize(obj);
}

Then you can use them like this in any query you like:

Enumerable.Range(1, 10)
.Select(i =>
    new
    {
        Index = i,
        IndexTimesTen = i * 10,
    })
.DumpJson();

I added "ToJson" separately so it can be used in with "Expessions".

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
  • 5
    That is nicer once it's set up. Thanks very much! I hope the author includes this in LinqPAD by default. Note to others, you will have to add the `System.Web.Extensions.dll` to the _My Extensions_ document, as described in my answer. – Drew Noakes Jul 11 '13 at 20:38
  • 1
    This fails if there is a relationship on the datatype with: A circular reference was detected while serializing an object of type 'LINQPad.User.YourType' :( – Leo Sep 16 '16 at 10:59
11

This is not directly supported, and I have opened a feature request here. Vote for it if you would also find this useful.

A workaround for now is to do the following:

  • Set the language to C# Statement(s)
  • Add an assembly reference (press F4) to System.Web.Extensions.dll
  • In the same dialog, add a namespace import to System.Web.Script.Serialization
  • Use code like the following to dump out your query as JSON
new JavaScriptSerializer().Serialize(query).Dump();
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
  • Thanks, this was the easiest solution to implement. – seangwright Aug 14 '15 at 14:43
  • The straightforward solution. – MarwaAhmad Nov 23 '16 at 02:53
  • Nice approach. Let me add, I had to append a `.select(...)` along with a `new {...}` like `var query = myTable.Select(s=> new {s.Id, s=>s.MyField1});` otherwise it would throw *`A circular reference was detected while serializing an object of type 'LINQPad.User.myTable'`* – Matt Jan 17 '19 at 13:37
11

There's a solution with Json.NET since it does indented formatting, and renders Json dates properly. Add Json.NET from NuGet, and refer to Newtonsoft.Json.dll to your “My Extensions” query and as well the following code :

public static object DumpJson(this object value, string description = null)
{
    return GetJson(value).Dump(description);
}

private static object GetJson(object value)
{
    object dump = value;

    var strValue = value as string;
    if (strValue != null)
    {
        var obj = JsonConvert.DeserializeObject(strValue);
        dump = JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.Indented);
    }
    else
    {
        dump = JsonConvert.SerializeObject(value, Newtonsoft.Json.Formatting.Indented);
    }

    return dump;
}

Use .DumpJson() as .Dump() to render the result. It's possible to override more .DumpJson() with different signatures if necessary.

Pierre Arnaud
  • 10,212
  • 11
  • 77
  • 108
Yang C
  • 536
  • 5
  • 16
2

As of version 4.47, LINQPad has the ability to export JSON built in. Combined with the new lprun.exe utility, it can also satisfy your needs.

http://www.linqpad.net/lprun.aspx

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807