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 6133707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:15:47+00:00 2026-05-23T17:15:47+00:00

I get JSON from a web service and can not influence the JSON format.

  • 0

I get JSON from a web service and can not influence the JSON format. The JSON code below is just an example to illustrate the problem. The field cars can either be an object containing Car objects or it can be an empty string. If I could change the web service, I’d change the empty String to be an empty object like "cars" : {} instead of "cars" : "".

When trying to map JSON to this Java object:

public class Person {
    public int id;
    public String name;
    public Map<String, Car> cars;
}

This works:

{
    "id" : "1234",
    "name" : "John Doe",
    "cars" : {
        "Tesla Model S" : {
            "color" : "silver",
            "buying_date" : "2012-06-01"
        },
        "Toyota Yaris" : {
            "color" : "blue",
            "buying_date" : "2005-01-01"
        }
    }
}

And this fails:

{
    "id" : "1",
    "name" : "The Dude",
    "cars" : ""
}

What would be the best way to handle this case in Jackson? If there’s the empty string, I’d like to get null for the field cars. I tried using ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, but it didn’t help.

  • 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-05-23T17:15:48+00:00Added an answer on May 23, 2026 at 5:15 pm

    The field cars can either contain a list of Car objects
    …
    This works:

    {
        "id" : "1234",
        "name" : "John Doe",
        "cars" : {
            "Tesla Model S" : {
                "color" : "silver",
                "buying_date" : "2012-06-01"
            },
            "Toyota Yaris" : {
                "color" : "blue",
                "buying_date" : "2005-01-01"
            }
        }
    }
    

    The “cars” element value is not a list (aka array). It’s a JSON object, which can also be considered a map-type collection, but it is not a list.

    So, to rephrase the issue, the goal is to deserialize JSON that is sometimes an object and sometimes an empty string into a Java Map.

    To solve this, I’m surprised ACCEPT_EMPTY_STRING_AS_NULL_OBJECT didn’t work. I recommend logging an issue at http://jira.codehaus.org/browse/JACKSON.

    You could implement custom deserialization. Following is an example solution. If the target data structure has other Map references, then this solution would need to be accordingly changed.

    import java.io.File;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.codehaus.jackson.JsonNode;
    import org.codehaus.jackson.JsonParser;
    import org.codehaus.jackson.JsonProcessingException;
    import org.codehaus.jackson.ObjectCodec;
    import org.codehaus.jackson.Version;
    import org.codehaus.jackson.map.DeserializationContext;
    import org.codehaus.jackson.map.JsonDeserializer;
    import org.codehaus.jackson.map.ObjectMapper;
    import org.codehaus.jackson.map.module.SimpleModule;
    import org.codehaus.jackson.type.TypeReference;
    
    public class Foo
    {
      public static void main(String[] args) throws Exception
      {
        SimpleModule module = new SimpleModule("CarsDeserializer", Version.unknownVersion());
        module.addDeserializer(Map.class, new CarsDeserializer());
    
        ObjectMapper mapper = new ObjectMapper().withModule(module);
    
        Person person1 = mapper.readValue(new File("input1.json"), Person.class);
        System.out.println(mapper.writeValueAsString(person1));
        // {"id":1234,"name":"John Doe","cars":{"Tesla Model S":{"color":"silver","buying_date":"2012-06-01"},"Toyota Yaris":{"color":"blue","buying_date":"2005-01-01"}}}
    
        Person person2 = mapper.readValue(new File("input2.json"), Person.class);
        System.out.println(mapper.writeValueAsString(person2));
        // {"id":1,"name":"The Dude","cars":{}}
      }
    }
    
    class Person
    {
      public int id;
      public String name;
      public Map<String, Car> cars;
    }
    
    class Car
    {
      public String color;
      public String buying_date;
    }
    
    class CarsDeserializer extends JsonDeserializer<Map<String, Car>>
    {
      @Override
      public Map<String, Car> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
          JsonProcessingException
      {
        ObjectCodec codec = jp.getCodec();
        JsonNode node = codec.readTree(jp);
        if (!"".equals(node.getTextValue()))
        {
          ObjectMapper mapper = new ObjectMapper();
          return mapper.readValue(node, new TypeReference<Map<String, Car>>() {});
        }
        return new HashMap<String, Car>(); // or return null, if preferred
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I speed it up? I get the results from JSON web service
What C-sharp type can I serialize to get JSON object with format name:[[1,2,3],[1,2,3],[1,2,3]] If
I have a web service that queries data from this json file, but I
I have a NSURLConnection that gets data from a JSON web service, and everything
I am pulling data from a web service and it is formatted as JSON.
I have a website where an ajax call will get some Json data from
When I try to use curl or file_get_contents to read something like http://example.com/python/json/ from
Trying to get a JSON output to work with jqGrid 'userdata' option. The example
While trying to GET a JSON, my callback function is NOT firing. $.ajax({ type:GET,
I am trying to get some errors returned in JSON format. So, I made

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.