504

Is it possible to return a dynamic object from a json deserialization using json.net? I would like to do something like this:

dynamic jsonResponse = JsonConvert.Deserialize(json);
Console.WriteLine(jsonResponse.message);
4

9 Answers 9

623

Json.NET allows us to do this:

dynamic d = JObject.Parse("{number:1000, str:'string', array: [1,2,3,4,5,6]}");

Console.WriteLine(d.number);
Console.WriteLine(d.str);
Console.WriteLine(d.array.Count);

Output:

 1000
 string
 6

Documentation here: LINQ to JSON with Json.NET

See also JObject.Parse and JArray.Parse

10
  • 43
    Note that for arrays the syntax is JArray.Parse.
    – jgillich
    May 15, 2014 at 10:24
  • 7
    Why do we need to use dynamic word ? i am scared never used before :D Aug 28, 2014 at 0:29
  • 4
    In VB.Net you need to do Dim d As Object = JObject.Parse("{number:1000, str:'string', array: [1,2,3,4,5,6]}")
    – ilans
    Dec 31, 2014 at 16:39
  • 3
    @MonsterMMORPG You should be :) Dynamic is an anti pattern in almost every circumstances, but, every now and then, you may have a situation where it's reasonable to use it.
    – Pluc
    Mar 18, 2016 at 18:30
  • 4
    With Newtonsoft.Json 8.0.3 (.NET 4.5.2): Microsoft.CSharp.RuntimeBinder.RuntimeBinderException occurred HResult=-2146233088 Message='Newtonsoft.Json.Linq.JObject' does not contain a definition for 'number' Source=Microsoft.CSharp StackTrace: at Microsoft.CSharp.RuntimeBinder.RuntimeBinderController.SubmitError(CError pError) May 4, 2016 at 11:17
119

As of Json.NET 4.0 Release 1, there is native dynamic support:

[Test]
public void DynamicDeserialization()
{
    dynamic jsonResponse = JsonConvert.DeserializeObject("{\"message\":\"Hi\"}");
    jsonResponse.Works = true;
    Console.WriteLine(jsonResponse.message); // Hi
    Console.WriteLine(jsonResponse.Works); // True
    Console.WriteLine(JsonConvert.SerializeObject(jsonResponse)); // {"message":"Hi","Works":true}
    Assert.That(jsonResponse, Is.InstanceOf<dynamic>());
    Assert.That(jsonResponse, Is.TypeOf<JObject>());
}

And, of course, the best way to get the current version is via NuGet.

Updated (11/12/2014) to address comments:

This works perfectly fine. If you inspect the type in the debugger you will see that the value is, in fact, dynamic. The underlying type is a JObject. If you want to control the type (like specifying ExpandoObject, then do so.

enter image description here

8
  • 25
    This never seems to work. It only returns a JObject, not a dynamic variable.
    – Paul
    Sep 9, 2014 at 17:52
  • 16
    BTW, this works: JsonConvert.DeserializeObject<ExpandoObject>(STRING); with proper deserialization, so we do not have JObject etc.
    – Gutek
    Nov 12, 2014 at 15:24
  • 2
    @Gutek not sure what your issue is. Did you run the code? I added asserts to the test and added a property not in the original json. Screenshot of the debugger included. Nov 12, 2014 at 16:19
  • 1
    @DavidPeden if you have JObject and you will try to bind that in Razor you will get exceptions. Question was about deserializing to dynamic object - JObject is dynamic but contains "own" types like JValue not primitive types. I can't use @Model.Prop name in Razor if return type is JValue.
    – Gutek
    Nov 17, 2014 at 12:32
  • 2
    This works, but each dynamic property is a JValue. Which confused me because I was working in the debugger/immediate window and wasn't seeing just strings. David shows this in the bottom screenshot. The JValue is convertible so you can just do string m = jsonResponse.message Feb 7, 2015 at 11:06
83

If you just deserialize to dynamic you will get a JObject back. You can get what you want by using an ExpandoObject.

var converter = new ExpandoObjectConverter();    
dynamic message = JsonConvert.DeserializeObject<ExpandoObject>(jsonString, converter);
2
  • 2
    The result can be also converted to a dictionary Jun 21, 2018 at 14:00
  • 1
    Exactly what I looked for! Thanks!
    – DarkDeny
    Aug 2, 2019 at 9:07
49

I know this is old post but JsonConvert actually has a different method so it would be

var product = new { Name = "", Price = 0 };
var jsonResponse = JsonConvert.DeserializeAnonymousType(json, product);
2
  • 28
    That would be deserializing a json payload into an anonymous type, not a dynamic type. Anonymous types and dynamic types are different things, and I don't believe this addresses the question asked.
    – jrista
    Aug 1, 2012 at 19:12
  • 1
    Is it necessary to use two variables? Why not reuse the first one in the second statement?
    – RenniePet
    Jul 23, 2013 at 1:06
39

Yes you can do it using the JsonConvert.DeserializeObject. To do that, just simple do:

dynamic jsonResponse = JsonConvert.DeserializeObject(json);
Console.WriteLine(jsonResponse["message"]);
3
  • 2
    JsonConvert doesn't contain a method called Deserialize. Nov 28, 2015 at 17:04
  • 1
    it should just be DeserializeObject, but this should be the accepted answer IMO
    – superjugy
    Dec 21, 2015 at 16:19
  • 1
    This works :D Thank you very much, all the other answers didn't work for me. I was trying to use DeserializeObject, however instead of jsonResponse["message"] I was using the syntax I normally use with regular class objects: jsonResponse.message
    – Sasino
    Dec 28, 2020 at 23:05
23

Note: At the time I answered this question in 2010, there was no way to deserialize without some sort of type, this allowed you to deserialize without having go define the actual class and allowed an anonymous class to be used to do the deserialization.


You need to have some sort of type to deserialize to. You could do something along the lines of:

var product = new { Name = "", Price = 0 };
dynamic jsonResponse = JsonConvert.Deserialize(json, product.GetType());

My answer is based on a solution for .NET 4.0's build in JSON serializer. Link to deserialize to anonymous types is here:

http://blogs.msdn.com/b/alexghi/archive/2008/12/22/using-anonymous-types-to-deserialize-json-data.aspx

5
  • I am with you phill don't know why people down-voting this, if any one can you please.. please explain why ?
    – PEO
    Jun 23, 2016 at 21:27
  • 21
    They are downvoting because the question is about deserializing without a type.
    – richard
    Aug 2, 2016 at 4:10
  • 4
    Answer was valid at the time of writing it in 2010 when there was no other solution. It was even the accepted answer for a small period of time until support came in JSON.NET.
    – Phill
    Mar 6, 2017 at 7:25
  • 1
    This doesn't produce a dynamic object. This produces a JObject which you reference as a dynamic. But its still a JObject inside. Jan 17, 2018 at 19:10
  • 1
    @ghostbust555 so, no different to dynamic d = JObject.Parse...
    – Phill
    Oct 6, 2020 at 6:24
7

If you use JSON.NET with old version which didn't JObject.

This is another simple way to make a dynamic object from JSON: https://github.com/chsword/jdynamic

NuGet Install

PM> Install-Package JDynamic

Support using string index to access member like:

dynamic json = new JDynamic("{a:{a:1}}");
Assert.AreEqual(1, json["a"]["a"]);

Test Case

And you can use this util as following :

Get the value directly

dynamic json = new JDynamic("1");

//json.Value

2.Get the member in the json object

dynamic json = new JDynamic("{a:'abc'}");
//json.a is a string "abc"

dynamic json = new JDynamic("{a:3.1416}");
//json.a is 3.1416m

dynamic json = new JDynamic("{a:1}");
//json.a is integer: 1

3.IEnumerable

dynamic json = new JDynamic("[1,2,3]");
/json.Length/json.Count is 3
//And you can use json[0]/ json[2] to get the elements

dynamic json = new JDynamic("{a:[1,2,3]}");
//json.a.Length /json.a.Count is 3.
//And you can use  json.a[0]/ json.a[2] to get the elements

dynamic json = new JDynamic("[{b:1},{c:1}]");
//json.Length/json.Count is 2.
//And you can use the  json[0].b/json[1].c to get the num.

Other

dynamic json = new JDynamic("{a:{a:1} }");

//json.a.a is 1.
1
  • this is very nice. i was looking for something like this for a while.... fills the brief exactly
    – jim tollan
    Jan 6, 2021 at 14:50
7

Yes it is possible. I have been doing that all the while.

dynamic Obj = JsonConvert.DeserializeObject(<your json string>);

It is a bit trickier for non native type. Suppose inside your Obj, there is a ClassA, and ClassB objects. They are all converted to JObject. What you need to do is:

ClassA ObjA = Obj.ObjA.ToObject<ClassA>();
ClassB ObjB = Obj.ObjB.ToObject<ClassB>();
0

It's actually pretty simple and neat using generic methods. For example:

TMap DeserializeAnonymous<TMap>(TMap obj, string content, JsonSerializerOptions serializerOptions = null)
{
    return JsonSerializer.Deserialize<TMap>(content, serializerOptions);
}

Usage:

string file = @"some\dir\file.json";
string content = File.ReadAllText(file);
var obj = DeserializeAnonymous(new
{
    SomeProp = "",
    SomeOtherProp = 0
}, content, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
obj.SomeProp;
etc....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.