Click or drag to resize
Json.NETCreating JSON
 

As well as parsing JSON from existing JSON strings, LINQ to JSON objects can be created from scratch to create new JSON structures.

Manually Creating JSON

Setting values and creating objects and arrays one at a time gives you total control, but it is more verbose than other options.

Creating JSON
 1JArray array = new JArray();
 2JValue text = new JValue("Manual text");
 3JValue date = new JValue(new DateTime(2000, 5, 23));
 4
 5array.Add(text);
 6array.Add(date);
 7
 8string json = array.ToString();
 9// [
10//   "Manual text",
11//   "2000-05-23T00:00:00"
12// ]
Creating JSON with LINQ

Declaratively creating JSON objects using LINQ is a fast way to create JSON from collections of values.

Creating JSON Declaratively
 1List<Post> posts = GetPosts();
 2
 3JObject rss =
 4    new JObject(
 5        new JProperty("channel",
 6            new JObject(
 7                new JProperty("title", "James Newton-King"),
 8                new JProperty("link", "http://james.newtonking.com"),
 9                new JProperty("description", "James Newton-King's blog."),
10                new JProperty("item",
11                    new JArray(
12                        from p in posts
13                        orderby p.Title
14                        select new JObject(
15                            new JProperty("title", p.Title),
16                            new JProperty("description", p.Description),
17                            new JProperty("link", p.Link),
18                            new JProperty("category",
19                                new JArray(
20                                    from c in p.Categories
21                                    select new JValue(c)))))))));
22
23Console.WriteLine(rss.ToString());
24
25//{
26//  "channel": {
27//    "title": "James Newton-King",
28//    "link": "http://james.newtonking.com",
29//    "description": "James Newton-King\'s blog.",
30//    "item": [
31//      {
32//        "title": "Json.NET 1.3 + New license + Now on CodePlex",
33//        "description": "Annoucing the release of Json.NET 1.3, the MIT license and being available on CodePlex",
34//        "link": "http://james.newtonking.com/projects/json-net.aspx",
35//        "category": [
36//          "Json.NET",
37//          "CodePlex"
38//        ]
39//      },
40//      {
41//        "title": "LINQ to JSON beta",
42//        "description": "Annoucing LINQ to JSON",
43//        "link": "http://james.newtonking.com/projects/json-net.aspx",
44//        "category": [
45//          "Json.NET",
46//          "LINQ"
47//        ]
48//      }
49//    ]
50//  }
51//}
Creating JSON from an object

The last option is to create a JSON object from a non-JSON type using the FromObject method. Internally, FromObject will use the JsonSerializer to serialize the object to LINQ to JSON objects instead of text.

The example below shows creating a JSON object from an anonymous object, but any .NET type can be used with FromObject to create JSON.

Creating JSON from an Object
 1JObject o = JObject.FromObject(new
 2{
 3    channel = new
 4    {
 5        title = "James Newton-King",
 6        link = "http://james.newtonking.com",
 7        description = "James Newton-King's blog.",
 8        item =
 9            from p in posts
10            orderby p.Title
11            select new
12            {
13                title = p.Title,
14                description = p.Description,
15                link = p.Link,
16                category = p.Categories
17            }
18    }
19});
See Also