1

The structure of the JSON response is in the format

Dictionary<string,Dictionary<string,double>>

which is to be deserialized.

Sometimes the inner dictionary is received as empty , {} which throws an error

Error converting value \"{}\" to type 'System.Collections.Generic.Dictionary`2[System.String,System.Double]'. Path '513215', line 1, position 14.

Even on trying below piece of code : it gives the same error

var settings = new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore,
                    MissingMemberHandling = MissingMemberHandling.Ignore
                };

var json_response = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, double>>>(response, settings);

How to handle this error and what are the best practices to handle it?

Example JSON which causes error

enter image description here

5
  • I don't see the JSON response your deserializing? Dictionary<string,Dictionary<string,double>> is just the type you want to deserialize to. Commented Feb 24, 2020 at 6:36
  • @RoadRunner Do you want example response or ? The last line of code is being used for deserialization Commented Feb 24, 2020 at 6:38
  • 1
    @Kitwradr Why do you have {} in the first place? That is an empty object. And {} is not null hence your settings will not work. Also, Json.NET expects a dictionary to be an object or null. Take a look at this question: stackoverflow.com/questions/45572652/… Commented Feb 24, 2020 at 7:09
  • Please, share the example json, which causes an error Commented Feb 24, 2020 at 10:02
  • Added the screenshot in the question Commented Feb 24, 2020 at 16:46

3 Answers 3

1

There is nothing stopping you from checking the response for empty set or bad value before you go thru this deserialization. I do it all the time. :)

Sign up to request clarification or add additional context in comments.

1 Comment

How do we check whether an inner dictionary is empty? @Rich K.
1

After seeing your JSON, I think the problem lies within you JSON itself. Not sure because you are using a visualizer. That "{...lots of stuff...}" is not going to be parsed correctly because you have quotes inside.

I would take your raw JSON over to https://jsonlint.com/ and see what it has to say. Once you get that worked out you probably will stop throwing errors. BTW your first "string" 513215 ... is not a string in the world of JSON, it's a number. It needs to be surrounded by quotes. Once again this could be the visualizer.

Don't forget about https://www.json.org/ to see all those JSON rules.

Comments

0

The best practice would be to not send empty dictionaries and should always send null instead of {}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.