Parse Snake case JSON in Spring Boot

Bhanu Chaddha
1 min readNov 21, 2018

--

If you are consuming a REST service in Spring Boot , you are using Jackson Objectmapper to parse the JSON into Java Object by default.

But there are cases when keys in JSON are not in camel case but in snake case.

Camel Case firstName

Snake Case first_name

Lets take an example, Suppose we need to create User object from incoming JSON :

public class User {
private String firstName ;
private String lastName ;
private String permanentAddress;
}

By default Jackson accept the the camel case thus , JSON in camel case would be parsed without any problem.

{
“firstName”: “Robert”,
“lastName”: “Svenson”,
“permanentAddress”: “B block, New York City”
}

But if the service you are consuming return JSON keys in snake case like below. We would need to add configuration to accept the snake case input.

{
“first_name” : “Robert”,
“last_name” : “Svenson”,
“permanent_address” : “B block, New York City”
}

Luckily its not that complicated.

We can either configure the whole application to expect the snake case input by adding below line in application.properties

spring.jackson.property-naming-strategy=SNAKE_CASE

Or you can add below annotation to model class if you want only specific model to accept and produce snake case JSON keys

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class User {
private String firstName ;
private String lastName ;
private String permanentAddress;
}

Similar Posts:

  1. Super Backend Man — How to become an awesome backend developer
  2. Using @JsonIgnore or @JsonProperty to hide sensitive data in JSON response

--

--

Bhanu Chaddha

Azure & DevOps Architect | Automation & Infrastructure Specialist | Experienced Kubernetes Practitioner | Speaker & Tech Blogger