0

I have JSON coming from 2 different sources that use different structures for the same object.

With JSON.Net, is it possible to have both of these json formats deserialize into the same class structure? Notice one uses JsonProperty and the other has the same property name where the default deserialization process would work.

Source A

    {
       "Fruit":"Apple"
    }

Source B

    {
       "f":"Apple"
    }

Into the same object...

public class Food
{
    [JsonProperty("f")]
    public string Fruit { get; set; }
}

And if the answer is yes, how is this done (code example would really be helpful!)?

John Livermore
  • 30,235
  • 44
  • 126
  • 216
  • You may need a customer converter like [this](https://www.jerriepelser.com/blog/deserialize-different-json-object-same-class/) or you may use a workaround like [this](https://stackoverflow.com/questions/43714050/multiple-jsonproperty-name-assigned-to-single-property/43715009) – Eldar May 21 '21 at 19:29

1 Answers1

0

The solution is to create a custom contract resolver as described in this post...

How to ignore JsonProperty(PropertyName = "someName") when serializing json?

I wasn't able to find this post without a lot of Google searching, so I am linking it here vs. deleting my post.

John Livermore
  • 30,235
  • 44
  • 126
  • 216