C# Deserialize JSON to dynamic ExpandoObject()

The Microsoft System.Text.Json.Serialization.JsonSerializer cannot (yet) convert JSON into dynamic objects. But good ole Newtonsoft.Json can.

The conversion is simple too:

// This is my json string
string s = "
    "label": "MyLabel",
    "values": {
      "Key1": "Value1",
      "Key2": "Value2",
      "Key2": "Value3",
// This is the conversion
dynamic json = 
    new ExpandoObjectConverter()

After the conversion, you can work with the dynamic object as you wish:

string label = json.label;
foreach (var value in json.values)
  Console.WriteLine($"{value.Key}: {value.Value}");


About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .net, .NET Core, c#, General .NET and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.