JSON parsing with spring boot

In this article, we will learn how to parse json string directly to a json object in spring boot and java applications with examples.

What is JSON?
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write and easy for machines to parse and generate.
JSON is a text format that is completely language independent and is now being widely used to exchange data between web applications.
JSON in Spring Boot
Spring Boot provides a number of features to make it easy to work with JSON data.
For example, it can automatically convert JSON data to and from Java objects. It can also provide support for JSONP (JSON with Padding), which allows JSON data to be retrieved from remote sources.
How to parse JSON in Spring Boot?
Parsing JSON in Spring Boot is very simple using Jackson library, which is included by default in the Spring boot starter dependencies.
All you need to do is add the following dependency to your project:

// MAVEN
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.7.5</version>
</dependency>

// GRADLE
implementation 'org.springframework.boot:spring-boot-starter-web:2.7.5'

This will automatically add Jackson dependencies to classpath.

If you are using Spring framework or the application does not use Spring and is written in plain java

// MAVEN
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.14.0</version>
</dependency>

// GRADLE
implementation 'com.fasterxml.jackson.core:jackson-databind:2.14.0'

Once you have the dependency in your project, you can use the Jackson library to parse JSON. The following example shows how to parse a JSON object using the Jackson library.

JSON Parser Example
The following is an example of a JSON parser written in Java. It uses the Jackson library to parse JSON data:

import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonParserExample {
  public static void main(String[] args) throws Exception {
    String jsonData = "{\"name\":\"John\",
                        \"age\":30,
                        \"address\":{
                             \"street\":\"123 Main Street\",
                             \"city\":\"New York\",
                             \"state\":\"NY\",
                             \"zip\":\"10001\"
                        },
                        \"phoneNumbers\":[
                            {\"type\":\"home\",
                             \"number\":\"212 555-1234\"
                            },
                            {\"type\":\"fax\",
                             \"number\":\"646 555-4567\"
                            }
                         ]
                   }";

    ObjectMapper mapper = new ObjectMapper();
    Person person = mapper.readValue(jsonData, Person.class);
    System.out.println(person);
  }
}

Remember that for this to work, the fields of corresponding java class must be same as the keys of json string and their data types should match with the type of values of the keys.
So, for the above example to work, the Person class should be

public class Person {
  private String name;
  private int age;
  private List<PhoneNumber> phoneNumbers;
  // getter and setter methods
}

public class PhoneNumber {
  private String type;
  private String number;
  // getter and setter methods
}

Following is an example of Spring boot controller where you are receiving json string directly inside a method parameter using @RequestBody annotation.

@PostMapping("user")
public String saveUser(@RequestBody String json) {
  ObjectMapper mapper = new ObjectMapper();
  MyObject obj = mapper.readValue(json, MyObject.class);
}

Benefits of JSON with Spring Boot
Using JSON with Spring Boot has a number of benefits including:

1. Automatic conversion of string to java object and vice-versa.
2. Spring Boot uses Jackson by default for serializing and deserializing JSON.
3. Jackson is a very popular and efficient Java library used for mapping objects to and from JSON.
4. Jackson has a number of features that make it very useful for working with JSON data in Spring Boot, such as:
A. Automatic type conversion
B. The ability to ignore null values
C. Support for a wide range of data types

Hope the article was useful.