2

I have a third-party API response with one very unpredictable property, the rest are OK.

Sometimes the property is a full nested object, sometimes it's a blank string, sometimes it is an array... The docs are not very good.

Here are some sample responses, but there could be more variations:

"errors": {
    "invalid_player_ids" : ["5fdc92b2-3b2a-11e5-ac13-8fdccfe4d986", "00cb73f8-5815-11e5-ba69-f75522da5528"]
  }

"errors": ["Notification content must not be null for any languages."]

"errors": ""

Luckily this property is not too important, but a nice-to-have for logging purposes.

Is it possible to deserialize the model as usual but for this particular property errors, deserialize the whole thing into a string property? Like so?

public string Errors { get; set; }
3
  • Can you give an example of the json you would receive, and the resulting data type (with values) you want in the end? Commented Mar 27, 2019 at 17:07
  • 3
    public dynamic Error { get; set; } might do it Commented Mar 27, 2019 at 17:10
  • I do not think dynamic is useful because I cannot predict structure, so I won't be able to read it. I would like to get a string of the object for that property. Commented Mar 27, 2019 at 17:43

2 Answers 2

4

I would use a JToken to handle the unpredictable property. It can handle any JSON, and if you need to log it out, you can just use ToString() to do it.

public class Response
{
    public JToken Errors { get; set; }
}

Then:

Response resp = JsonConvert.DeserializeObject<Response>(json);
Console.WriteLine("Errors: " + resp.Errors.ToString());

Here is a working demo: https://dotnetfiddle.net/5jXHjV

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

1 Comment

Excellent, that did what I needed. Thank you.
2

Like @stuartd has stated, a dynamic property also does the same thing.

dotNetFiddle: https://dotnetfiddle.net/dVzsZm

Here's the working code.

I created a helper readonly property that returns ToString of the dynamic property. You could also do without it.

using System;
using Newtonsoft.Json;

namespace DynamicErrorsJson
{
    public class ApiResponse
    {
        public dynamic Errors { get; set; }

        public string ErrorsString
        {
            get
            {
                string value = string.Empty;
                if (Errors != null)
                {
                    value = Errors.ToString();
                }

                return value;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var listErrorsJson = @"{ ""errors"": {""invalid_player_ids"" : [""5fdc92b2-3b2a-11e5-ac13-8fdccfe4d986"", ""00cb73f8-5815-11e5-ba69-f75522da5528""] } }";
            var stringErrorsJson = @"{ ""errors"": [""Notification content must not be null for any languages.""] }";
            var noErrorsJson = @"{""errors"": """" }";

            var listErrorsResponse = JsonConvert.DeserializeObject<ApiResponse>(listErrorsJson);
            var stringErrorsJsonResponse = JsonConvert.DeserializeObject<ApiResponse>(stringErrorsJson);
            var noErrorsJsonResponse = JsonConvert.DeserializeObject<ApiResponse>(noErrorsJson);

            Console.WriteLine("listErrorsJson Response: {0}\n\t", listErrorsResponse.ErrorsString);
            Console.WriteLine("stringErrorsJson Response: {0}\n\t", stringErrorsJsonResponse.ErrorsString);
            Console.WriteLine("noErrorsJson Response: {0}\n\t", noErrorsJsonResponse.ErrorsString);
            Console.WriteLine();
            Console.WriteLine("Press a key to exit...");

            Console.ReadKey();
        }
    }
}

Here's the output

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.