Deserialize JSON object into dynamic object using Json.net
Asked Answered
F

9

505

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);
Folkrock answered 26/12, 2010 at 23:24 Comment(4)
Consider to generate C# class from JSON json2csharp.com and use generated class instead of dynamicScratch
Possible duplicate of Deserialize JSON into C# dynamic object?Bristow
How do you suggest stackOverflow close a question as "too old"? It's been six years, there are valid answers and reasonable suggestions for every version of .net since then... so many that they aren't really helpful any more.Boaten
Does this help? https://mcmap.net/q/75228/-deserializing-json-response-without-creating-a-classLonni
A
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

Arkwright answered 23/1, 2012 at 13:4 Comment(10)
Note that for arrays the syntax is JArray.Parse.Microclimate
Why do we need to use dynamic word ? i am scared never used before :DWelladvised
In VB.Net you need to do Dim d As Object = JObject.Parse("{number:1000, str:'string', array: [1,2,3,4,5,6]}")Dominique
@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.Proglottis
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)Daveta
Doesn't seem to work in version 10.0.1. You get the runtime binder exception. Answer is false.Sulemasulf
Note: you should add the reference Microsoft.CSharp.dll to make use of d.number etc..Wealth
This is not/no longer (?) returning dynamic but a JObject instead (public static JObject Parse(string json, JsonLoadSettings settings);). Docs here: newtonsoft.com/json/help/html/LINQtoJSON.htmPule
@Pule You can still declare the type as dynamic and then access using property notation as opposed to having to access everything via indexers. The answer is correct.Heyman
10 years later, it's still the best answer.Accusal
C
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

Clearance answered 6/5, 2012 at 4:27 Comment(8)
This never seems to work. It only returns a JObject, not a dynamic variable.Lifeblood
BTW, this works: JsonConvert.DeserializeObject<ExpandoObject>(STRING); with proper deserialization, so we do not have JObject etc.Pragmaticism
@Pragmaticism 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.Clearance
@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.Pragmaticism
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.messagePatron
Exactly what I was looking for, works fine for me, many thanks!Syrinx
@Pragmaticism 7 years ago, but still its the answer, thank you :)Tenace
@Tenace ah, if only you had waited 3 more days to comment...Clearance
N
84

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);
Needlework answered 19/9, 2014 at 18:33 Comment(2)
The result can be also converted to a dictionaryNamnama
Exactly what I looked for! Thanks!Priapus
V
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);
Valetudinarian answered 12/2, 2011 at 21:18 Comment(2)
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.Independence
Is it necessary to use two variables? Why not reuse the first one in the second statement?Supposing
C
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"]);
Communard answered 16/1, 2014 at 12:6 Comment(3)
JsonConvert doesn't contain a method called Deserialize.Deoxyribonuclease
it should just be DeserializeObject, but this should be the accepted answer IMOKoine
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.messageObsolescent
F
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

Folia answered 26/12, 2010 at 23:41 Comment(5)
I am with you phill don't know why people down-voting this, if any one can you please.. please explain why ?Unrequited
They are downvoting because the question is about deserializing without a type.Gourde
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.Folia
This doesn't produce a dynamic object. This produces a JObject which you reference as a dynamic. But its still a JObject inside.Appledorf
@Appledorf so, no different to dynamic d = JObject.Parse...Folia
L
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.
Laboured answered 16/1, 2016 at 4:12 Comment(1)
this is very nice. i was looking for something like this for a while.... fills the brief exactlyLawson
M
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>();
Malikamalin answered 13/1, 2018 at 13:4 Comment(0)
G
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....
Gerick answered 17/1 at 20:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.