9

I have a json file which looks like this:

{
    "ANIMALS": {
    "TYPE": "MAMMAL",
    "COLOR": "BLACK",
    "HEIGHT": "45",

    }
}

But I get property not found error. If I change it to animals(lowercase). it works fine. Can anyone suggest me the model class for this sample json file which will get parsed correctly.

Deepak Senapati
  • 1,103
  • 2
  • 11
  • 30

4 Answers4

13

Building off of Deepak's answer, depending on how you have Jackson configured, you may need to put the @JsonProperty on the getters & setters instead of the property or you might get duplicate properties in the resulting JSON.

Example

 @JsonProperty("ANIMALS")
 private string animals;

Results in...{animals:"foo",ANIMALS:"foo"}

 private string animals;

 @JsonProperty("ANIMALS")
 public String getAnimals(){...}

Results in...{ANIMALS:"foo"}

Snekse
  • 15,474
  • 10
  • 62
  • 77
10

You should implement new naming strategy for your case:

class UpperCaseNamingStrategy extends PropertyNamingStrategy.PropertyNamingStrategyBase {

    private static final long serialVersionUID = 1L;

    @Override
    public String translate(String arg0) {
        return arg0.toUpperCase();
    }
}

After that, configure ObjectMapper

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setPropertyNamingStrategy(new UpperCaseNamingStrategy());

See also @JsonProperty annotation.

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • Thanks for your answer. I solved this issue using @JsonProperty annotation.Saw your answer after that but since you gave the right answer, I will accept it. :) – Deepak Senapati May 22 '13 at 07:19
  • 1
    Thanks! Just one question....why did you call it `LowerCaseNamingStrategy` when it is for dealing with **upper** case variables? :) – Adam Burley Jan 23 '21 at 22:32
  • 1
    @AdamBurley, good point, I changed a name to `Upper...`. I do not know why 7 years ago I named it `Lower...`, probably because I extend `LowerCaseWithUnderscoresStrategy` and just copied this name. – Michał Ziober Jan 23 '21 at 23:03
  • 1
    No worries! Actually I think it's fine to just extend `PropertyNamingStrategy.PropertyNamingStrategyBase`; I don't see any benefit in extending `LowerCaseWithUnderscoresStrategy`... – Adam Burley Jan 24 '21 at 03:25
  • @AdamBurley, fixed, thanks for these suggestions. – Michał Ziober Jan 24 '21 at 10:54
  • 2
    Me again! Just found out that as of Jackson 2.12 (Nov 2020), `PropertyNamingStrategy.PropertyNamingStrategyBase` is deprecated. Need to use `PropertyNamingStrategies.NamingBase` instead, which is basically identical - but not deprecated :) – Adam Burley Jan 25 '21 at 01:58
3

Thanks I solved this issue using @JsonProperty annotation

    @JsonProperty("ANIMALS")
    private string animals;
Deepak Senapati
  • 1,103
  • 2
  • 11
  • 30
1

If you are not generating the JSON (serialisation), but you want to consume an object without having to care about the case.

You can receive Animal or AniMal :

ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);