© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•16mo ago•
72 replies
UnemployedNinja

Best way to deserialize duplicate JSON API responses (Using Newtonsoft.JSON)

I'm using a HTTP client to contact an API, and then deserializing the JSON response into an instance of its respective object type.

My issue is this - the API has two versions:
- Version 1: The old API, but still active, however, it's not maintained and may be missing bits of data that are present in requests made to v2 of the API.
- Version 2: The new API. Almost identical responses to v1, but all of the fields are obfuscated.

So an API response for version 1 and 2 may look like this:
Version 1:
{
  "id": "1"
  "name": "John"
  "bleh": "Something else"
}
{
  "id": "1"
  "name": "John"
  "bleh": "Something else"
}

Version 2:
{
  "lLL": "1"
  "llLLlLLl": "John"
  "LLlLLllL": "Something else"
}
{
  "lLL": "1"
  "llLLlLLl": "John"
  "LLlLLllL": "Something else"
}


If I were to to create a class for this response, it may look like this:
public class Person {
  [JsonProperty("id")]
  public readonly int Id;

  [JsonProperty("name")]
  public readonly string Name;

  [JsonProperty("bleh")]
  public readonly string Bleh;

  public Person() {}
}
public class Person {
  [JsonProperty("id")]
  public readonly int Id;

  [JsonProperty("name")]
  public readonly string Name;

  [JsonProperty("bleh")]
  public readonly string Bleh;

  public Person() {}
}


As you can see, this makes it difficult to reuse the same class type for v2 data. Is there a "clean" way I to achieve using only on class type for deserialization of both API versions?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

Newtonsoft.Json deserialize issue [Answered]
C#CC# / help
4y ago
Deserialize JSON to dynamic object without Newtonsoft.Json
C#CC# / help
4y ago
How to deserialize dictionary using Newtonsoft [Answered]
C#CC# / help
4y ago