3

I am talking to an webservice that that can return the following JSON structure:

{
    "foo": {
        "bar": "hello world"
    }
}

Foo is optional, but instead of the value NULL, we get the following:

{
    "foo": []
}

An empty array. I used an (ugly) work around for this by using the following properties in my model:

public dynamic Foo { get; set; }
public FooModel FooObject {
    get
    {
        if(Foo == null || Foo.GetType().IsArray)
        {
            return null;
        }
        return ((JObject)Foo).ToObject<FooModel>();
    }
}

This works for this single property. But the webservice does this for all objects that are NULL. I'm looking for a generic solution that ignores all empty arrays when deserialization. (or something else)

I haven't been able to find a solution. I tried looking into using an custom JsonConverter and ContractResolver.

6
  • Have you looked here for ideas: stackoverflow.com/a/18997172/226781 Commented Jun 5, 2018 at 14:30
  • @asherber Yea I have seen it, it would be a possible solution, but it would require to change all my properties in my models to arrays/lists and put attributes on them all. Wouldn't be much better than my current workaround. Commented Jun 5, 2018 at 14:41
  • Another possibly related question: Deserialize JSON when type can be different. But if you say you have to do it for every possible property including those with primitive values, the accepted answer may not work. Commented Jun 5, 2018 at 20:47
  • 1
    Alternatively, pre-load your JSON into a JToken and use RemoveEmptyArrayProperties from Woo commerce json to Dataset or datatable. Commented Jun 5, 2018 at 21:10
  • @dbc, the RemoveEmptyArrayProperties worked! Thx. will you answer or shall I create a Community Wiki answer? Commented Jun 6, 2018 at 6:17

1 Answer 1

1

One simple way do avoid the problem is to pre-load the JSON into a JToken then remove all empty array properties using the extension method RemoveEmptyArrayProperties from this answer to Woo commerce json to Dataset or datatable:

public static class JsonExtensions
{
    public static TJToken RemoveEmptyArrayProperties<TJToken>(this TJToken root) where TJToken : JToken
    {
        var container = root as JContainer;
        if (container == null)
            return root;
        var query = container.DescendantsAndSelf()
            .OfType<JProperty>()
            .Where(p => p.Value is JArray && ((JArray)p.Value).Count == 0);
        foreach (var property in query.ToList())
        {
            property.Remove();
        }

        return root;
    }
}

Given the method, you can pre-process your JSON string as follows:

var root = JObject.Parse(jsonString)
    .RemoveEmptyArrayProperties()
    .ToObject<RootObject>();

Sample working .Net fiddle here.

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

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.