Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8548417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:29:27+00:00 2026-06-11T13:29:27+00:00

I am using an API that returns a json object that I need to

  • 0

I am using an API that returns a json object that I need to deserialize. My problem is that one of the members of those object is sometimes an empty array (“[]”) and sometimes a dictionary (“{“1”:{…}, “2”:{…}}”). I want to deserialize it into either an array or a dictionary, since I don’t car about the IDs, I just want a list of all the objects. Here is how I deserialize the object:

var response = JsonConvert.DeserializeObject<Response>(json);

And here is the definition of the Response class:

public class Response
{
    [JsonProperty(PropertyName = "variations")]
    public Dictionary<int, Variation> Variations { get; set; }
}

It works well when the Response contains a dictionary in it’s variations field, but it fails when it contains an empty array. I’m getting an error from Newtonsoft saying that an array cannot be deserialized into a dictionary. If I define the Variations property as an array, it works for empty arrays, but it fails when it is a dictionary. What could I do to either deserialize correctly both possible values, or to ignore empty arrays and set Variations to null when it’s an array instead of failing.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:29:29+00:00Added an answer on June 11, 2026 at 1:29 pm

    Here is the solution I used :

        public Dictionary<int, Variation> Variations
        {
            get
            {
                var json = this.VariationsJson.ToString();
                if (json.RemoveWhiteSpace() == EmptyJsonArray)
                {
                    return new Dictionary<int, Variation>();
                }
                else
                {
                    return JsonConvert.DeserializeObject<Dictionary<int, Variation>>(json);
                }
            }
        }
    
        [JsonProperty(PropertyName = "variations")]
        public object VariationsJson { get; set; }
    

    Basically, the variations are first deserialized in a basic object. When I want to read the value, I check if the object is an empty array, and if so I return an empty dictionary. If the object is a good dictionary, I deserialize it and return it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am calling an API that returns JSON data. I am using json.loads to
Youtube's API returns a JSON object with an array of videos. Each video object
I have made an api that returns an object as json data. I Am
I have a json object that I'm loading from wordpress using the JSON API
I'm using WCF Data Services for a RESTful API that returns a JSON response.
I'm working with an API that returns a JSON dictionary. One of the keys
Situation: I need to make an imap client (using java mail api) that if,
I understand that using the facebook API I need an api key to connect,
The api I'm calling returns a json object of validation errors with the HTTP
Follow up to this question for Facebook Friends.getAppUsers using Graph API that pulls friends

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.