Json.NET: Deserializing nested dictionaries

29,799

Solution 1

I found a way to convert all nested objects to Dictionary<string,object> by providing a CustomCreationConverter implementation:

class MyConverter : CustomCreationConverter<IDictionary<string, object>>
{
    public override IDictionary<string, object> Create(Type objectType)
    {
        return new Dictionary<string, object>();
    }

    public override bool CanConvert(Type objectType)
    {
        // in addition to handling IDictionary<string, object>
        // we want to handle the deserialization of dict value
        // which is of type object
        return objectType == typeof(object) || base.CanConvert(objectType);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.StartObject
            || reader.TokenType == JsonToken.Null)
            return base.ReadJson(reader, objectType, existingValue, serializer);

        // if the next token is not an object
        // then fall back on standard deserializer (strings, numbers etc.)
        return serializer.Deserialize(reader);
    }
}

class Program
{
    static void Main(string[] args)
    {
        var json = File.ReadAllText(@"c:\test.json");
        var obj = JsonConvert.DeserializeObject<IDictionary<string, object>>(
            json, new JsonConverter[] {new MyConverter()});
    }
}

Documentation: CustomCreationConverter with Json.NET

Solution 2

I had a very similar but slightly more complex need when I ran across this Q. At first I thought maybe I could adapt the accepted answer, but that seemed a bit complicated and I ended up taking a different approach. I was attempting to put a modern JSON layer on top of a legacy C++ API. I'll spare you the details of that, and just say the requirements boil down to:

  • JSON objects become Dictionary<string,object>.

  • JSON arrays become List<object>.

  • JSON values become the corresponding primitive CLR values.

  • The objects and arrays can be infinitely nested.

I first deserialize the request string into a Newtonsoft JSON object and then call my method to convert in accordance with the above requirements:

var jsonObject = JsonConvert.DeserializeObject(requestString);
var apiRequest = ToApiRequest(jsonObject);
// call the legacy C++ API ...

Here is my method that converts to the structure the API expects:

    private static object ToApiRequest(object requestObject)
    {
        switch (requestObject)
        {
            case JObject jObject: // objects become Dictionary<string,object>
                return ((IEnumerable<KeyValuePair<string, JToken>>) jObject).ToDictionary(j => j.Key, j => ToApiRequest(j.Value));
            case JArray jArray: // arrays become List<object>
                return jArray.Select(ToApiRequest).ToList();
            case JValue jValue: // values just become the value
                return jValue.Value;
            default: // don't know what to do here
                throw new Exception($"Unsupported type: {requestObject.GetType()}");
        }
    }

I hope that someone can find this approach useful.

Solution 3

Alternative/Update:

I needed to deserialize a dictionary of dictionaries of Strings and with current Json.NET (5.0) I did not had to create a CustomConverter, I just used (in VB.Net):

JsonConvert.DeserializeObject(Of IDictionary(Of String, IDictionary(Of String, String)))(jsonString)

Or, in C#:

JsonConvert.DeserializeObject<IDictionary<String, IDictionary<String, String>>(jsonString);

Solution 4

@AlexD's accepted solution does not work ideally if there is an array in the json. It returns a JArray of JObject instead a List<Dictionary<string, object>>

This can be solved by modifying the ReadJson() method:

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
    if (reader.TokenType == JsonToken.StartObject || reader.TokenType == JsonToken.Null)
        return base.ReadJson(reader, objectType, existingValue, serializer);

    //if it's an array serialize it as a list of dictionaries
    if(reader.TokenType == JsonToken.ArrayStart)
        return serializer.Deserialize(reader, typeof(List<Dictionary<string, object>>));    


    // if the next token is not an object
    // then fall back on standard deserializer (strings, numbers etc.)
    return serializer.Deserialize(reader);
}

Solution 5

I have a nested/deep structure of "unknown" dictionaries that is serialized/deserialized to/from C# objects and JSON string. .NET 5.

If I use Newtonsoft it does not work automatically.

If I use System.Text.Json it works automatically.

//does NOT work (newtonDeserialized does not have the same data in the nested Dictionaries as in object):
var newtonSerialized = Newtonsoft.Json.JsonConvert.SerializeObject(object);
var newtonDeserialized = Newtonsoft.Json.JsonConvert.DeserializeObject<WaitlistResponse>(newtonSerialized);

//Works (netDeserialized have the same data in the nested Directories as in object):
var netSerialized = System.Text.Json.JsonSerializer.Serialize(object);
var netDeserialized = System.Text.Json.JsonSerializer.Deserialize<WaitlistResponse>(netSerialized);
Share:
29,799
Daniel
Author by

Daniel

Updated on July 19, 2022

Comments

  • Daniel
    Daniel almost 2 years

    When deserializing an object to a Dictionary (JsonConvert.DeserializeObject<IDictionary<string,object>>(json)) nested objects are deserialized to JObjects. Is it possible to force nested objects to be deserialized to Dictionarys?

    • zirkelc
      zirkelc about 6 years
      This article provides an easy way to deserialize a nested JSON obejct into a Dictionary: buildplease.com/pages/json
    • alans
      alans over 5 years
      May not be Dictionary, but I myself have been deserializing to ExpandoObject to get the same effect.
  • Andrew Theken
    Andrew Theken about 9 years
    This does not support recursive/unknown json structures being converted properly.
  • Javier
    Javier almost 9 years
    This doesn't answer, as it specifically refers to a fixed levels of nesting
  • Pushpa Kumara
    Pushpa Kumara almost 3 years
    This was really useful. I was unable to decode this kind of response with the given solution. {"id":2,"name":"root","descendants":[{"id":4,"name":"node2",‌​"descendants":[{"id"‌​:7,"name":"node21"},‌​{"id":8,"name":"node‌​22"}]},{"id":3,"name‌​":"node1","descendan‌​ts":[{"id":5,"name":‌​"node11"},{"id":6,"n‌​ame":"node12"}]}]}. Thanks