50

Is there a way to make Jackson interpret single JSON object as an array with one element and vice versa?

Example, I have 2 slightly different formats of JSON, I need both to map to same Java object:

Format A (JSON array with one element):

points : [ {
    date : 2013-05-11
    value : 123
}]

Format B (JSON object, yes I know it looks "wrong" but it's what I'm given):

points : {
    date : 2013-05-11
    value : 123
}

Target Java object that both of the above should convert to:

//Data.java 
public List<Point> points;
//other members omitted

//Point.java
class Point {
    public String date;
    public int value;
}

Currently, only A will parse properly to Data. I want avoid directly tampering with the JSON itself. Is there some configuration in Jackson I can tamper with in order to make it accept B ?

3 Answers 3

95

Try with DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY - it should work for you.

Example:

final String json = "{\"date\" : \"2013-05-11\",\"value\" : 123}";

final ObjectMapper mapper = new ObjectMapper()
        .enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
final List<Point> points = mapper.readValue(json,
        new TypeReference<List<Point>>() {});
Sign up to request clarification or add additional context in comments.

4 Comments

Looks to be exactly what I'm looking for. I'll try this out tomorrow. Thanks.
It doesn't exist in Jackson 1.7.9. What is the relationship between that and the link you sent?
You should upgrade Jackson library to at least version 2.x.x.
It is also possible to enable it on a per-field basis, using @JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
9

The Jackson 1.x-compatible version uses DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY. So the above answer changes to:

final String json = "{\"date\" : \"2013-05-11\",\"value\" : 123}";

final ObjectMapper mapper = new ObjectMapper()
    .enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
final List<Point> points = mapper.readValue(json,
    new TypeReference<List<Point>>() {
    });
System.out.println(points);

Comments

0

Can solve the above problem by this code is given below, this works

 final ObjectMapper objectMapper = new 
 ObjectMapper().enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);

 objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
 try {
      String jsonInString = objectMapper.writeValueAsString(products.get(j));
      InventoryParser inventoryParser = objectMapper.readValue(jsonInString, 
                                       InventoryParser.class);

      System.out.println(inventoryParser.getId());

     } 
     catch (IOException e) 
     {
       e.printStackTrace();
     }

"InventoryParser" is a POJO Class. "products.get(j)" is JSON String.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.