LINQ to JSON beta

Oh no, Beta! I like LINQ. A lot. For the past few months I have been fortunate to work on a .NET 3.5 project and I have been making heavily use of LINQ to XML. While using XLINQ it occurred to me that a similar API for working with JSON objects, which share a lot of similarities with XML, would be useful in many situations.

This beta is a very rough early preview of LINQ to JSON. It was literally written in a weekend [:)] The aim is to get feedback and ideas on how the API could be improved.

What is LINQ to JSON

LINQ to JSON isn't a LINQ provider but rather an API for working with JSON objects. The API has been designed with LINQ in mind to enable the quick creation and querying of JSON objects.

LINQ to JSON is an addition to Json.NET. It sits under the Newtonsoft.Json.Linq namespace.

Creating JSON

Creating JSON using LINQ to JSON can be done in a much more declarative manner than was previously possible.

List<Post> posts = GetPosts();
 
JObject rss = 
  new JObject(
    new JProperty("channel",
      new JObject(
        new JProperty("title", "James Newton-King"),
        new JProperty("link", "http://james.newtonking.com"),
        new JProperty("description", "James Newton-King's blog."),
        new JProperty("item",
          new JArray(
            from p in posts
            orderby p.Title
            select new JObject(
              new JProperty("title", p.Title),
              new JProperty("description", p.Description),
              new JProperty("link", p.Link),
              new JProperty("category",
                new JArray(
                  from c in p.Categories
                  select new JValue(c)))))))));
 
Console.WriteLine(rss.ToString());
 
//{
//  "channel": {
//    "title": "James Newton-King",
//    "link": "http://james.newtonking.com",
//    "description": "James Newton-King's blog.",
//    "item": [
//      {
//        "title": "Json.NET 1.3 + New license + Now on CodePlex",
//        "description": "Annoucing the release of Json.NET 1.3, the MIT license and the source being available on CodePlex",
//        "link": "http://james.newtonking.com/projects/json-net.aspx",
//        "category": [
//          "Json.NET",
//          "CodePlex"
//        ]
//      },
//      {
//        "title": "LINQ to JSON beta",
//        "description": "Annoucing LINQ to JSON",
//        "link": "http://james.newtonking.com/projects/json-net.aspx",
//        "category": [
//          "Json.NET",
//          "LINQ"
//        ]
//      }
//    ]
//  }
//}

Querying JSON

LINQ to JSON is designed with LINQ querying in mind. These examples use the RSS JSON object created previously.

Getting the post titles:

var postTitles =
  from p in rss.PropertyValue<JObject>("channel")
              .PropertyValue<JArray>("item")
              .Children<JObject>()
  select p.PropertyValue<string>("title");
 
foreach (var item in postTitles)
{
  Console.WriteLine(item);
}
 
//LINQ to JSON beta
//Json.NET 1.3 + New license + Now on CodePlex

Getting all the feed categories and a count of how often they are used:

var categories =
  from c in rss.PropertyValue<JObject>("channel")
              .PropertyValue<JArray>("item")
              .Children<JObject>()
              .PropertyValues<JArray>("category")
              .Children<string>()
  group c by c into g
  orderby g.Count() descending
  select new { Category = g.Key, Count = g.Count() };
 
foreach (var c in categories)
{
  Console.WriteLine(c.Category + " - Count: " + c.Count);
}
 
//Json.NET - Count: 2
//LINQ - Count: 1
//CodePlex - Count: 1

Feel free to make suggestions and leave comments. I am still experimenting with different ways of working with JSON objects and their values.

Json.NET CodePlex Project

LINQ to JSON - Json.NET 2.0 Beta - Json.NET source code and binaries

 

kick it on DotNetKicks.com