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

  • SEARCH
  • Home
  • 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 8072015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:52:40+00:00 2026-06-05T13:52:40+00:00

I am working with an external API that returns a property either as an

  • 0

I am working with an external API that returns a property either as an array or as an object, depending on the count. What is a good way to handle this?

Returning as array:

{
    "contacts": {
        "address": [
            {
                "id": "47602070",
                "type": "Work",
                "street": "MyStreet",
                "city": "MyCity",
                "zip": "12345",
                "country": "USA"
            },
            {
                "id": "47732816",
                "type": "GPS",
                "street": "50.0,30.0"
            }
        ]
    }
}

Returning as object:

{
    "contacts": {
        "address": {
            "id": "47602070",
            "type": "Work",
            "street": "MyStreet",
            "city": "MyCity",
            "zip": "12345",
            "country": "USA"
        }
    }
}

I’m thinking a workaround would be to use a custom deserializer and return an array of length 1 for the object case, and default deserialization for the array case, but I don’t know how to do that yet.

I tried deserializing the object to an array and hoping Json.net would handle this case for me, but no dice.

  • 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-05T13:52:42+00:00Added an answer on June 5, 2026 at 1:52 pm

    A custom JSON.NET converter might do the trick here. It’s not that hard.

    For a DateTime property you might do it as follows. Just decorate the property in question with the custom converter.

    [JsonObject(MemberSerialization.OptIn)]
    public class MyClass
    {
        [JsonProperty(PropertyName = "creation_date")]
        [JsonConverter(typeof(UnixDateTimeConverter))]
        public DateTime CreationDate { get; set; }
    }
    

    JSON.NET provides most of the plumbing. Just derive from a base converter.

    public class UnixDateTimeConverter : DateTimeConverterBase
    {
        public override void WriteJson(JsonWriter writer, object value, 
                                       JsonSerializer serializer)
        { ...}
    
        public override void WriteJson(JsonWriter writer, object value, 
                                       JsonSerializer serializer)
        { ... } 
    }  
    

    All you have to do is implement the ReadJson (deserialization) and WriteJson (serialization) methods.

    You can find a complete example here:

    Writing a custom Json.NET DateTime Converter

    For your particular problem you need a bit more control. Try the following type of converter:

    public class Contact
    { 
       private List<Address> _addresses = new List<Address>();       
       public IEnumerable<Address> Addresses { get { return _addresses; }
    }
    
    public class ContactConverter : CustomCreationConverter<Contact>
    {
        public override Contact Create(Type objectType)
        {
            return new Contact();
        }
    
        public override object ReadJson(JsonReader reader, Type objectType, object 
            existingValue, JsonSerializer serializer)
        {
            var mappedObj = new Contact();
    
            // Parse JSON data here
            // ...
    
            return mappedObj;
        }
    }
    

    Using a custom converter like the one above you can parse the JSON data yourself and compose the Contact object(s) as you please.

    I modified an example I found here:

    JSON.NET Custom Converters–A Quick Tour

    In this case you need to pass the custom converter when desearilizing.

    Contact contact = 
        JsonConvert.DeserializeObject<Contact>(json, new ContactConverter());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an external REST API that returns some data as a comma-separated
I'm working on a system that interacts with many external system API:s. Most of
I'm working with an API that requires the machine's external IP. I have come
I'm working in jQuery and I want to query an external XML API that's
I'm working on an API that sends HTML forms to external AJAX apps. Would
i'm working on a simple application that loads an external url (google.com) into a
The preamble We're implementing a MVC2 site that needs to consume an external API
I am working on building an iPhone application that uses an external server to
I am working on a PHP script that makes an API call to a
In a project I'm working on, I'm requesting data from an external API in

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.