In this tutorial we will learn how to parse JSON using the ObjectMapper API in a Spring Boot application.

The Jackson com.fasterxml.jackson.databind.ObjectMapper class is a simple way to parse and create JSON. The Jackson ObjectMapper can parse JSON from a string, stream or file, and create a Java object or object graph representing the parsed JSON.

Let's see quick example:

package com.example.demojson;

import java.io.File;
import java.util.List;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.fasterxml.jackson.core.JsonParser.Feature;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
	@Bean
	CommandLineRunner runner() {
		return args -> {
			 
			String json = " {\n" + 
					"    \"name\": \"Edmund lronside\",\n" + 
					"    \"city\": \"United Kingdom\",\n" + 
					"    \"house\": \"House of Wessex\",\n" + 
					"    \"years\": \"1016\"\n" + 
					"  }";
			ObjectMapper mapper = new ObjectMapper();
			King c = mapper.readValue(json, King.class);
			System.out.println(c);
			
			
		};
	}
}

In the above example, we are parsing a simple JSON String and converting it into a Java class:

package com.example.demojson;

public class King {
	String name;
	String city;
	String house;
	String years;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getHouse() {
		return house;
	}
	public void setHouse(String house) {
		this.house = house;
	}
	public String getYears() {
		return years;
	}
	public void setYears(String years) {
		this.years = years;
	}
	public King() {
		super();
		// TODO Auto-generated constructor stub
	}
	public King(String name, String city, String house, String years) {
		super();
		this.name = name;
		this.city = city;
		this.house = house;
		this.years = years;
	}
	@Override
	public String toString() {
		return "King [name=" + name + ", city=" + city + ", house=" + house + ", years=" + years + "]";
	}
	 
}

In the following example, we are parsing a JSON external file and converting it into a List of Java objects:

TypeReference<List<King>> typeReference = new TypeReference<List<King>>(){};
List<King> list= mapper.readValue(new File("/tmp/sample.json"), typeReference);
for (King k: list)
System.out.println(k);

Notice the TypeReference parameter passed to readValue(). This parameter tells Jackson to read a List of King objects.

You can also use the ObjectMapper API to produce JSON Strings into a File, as in the following example:

ObjectMapper objectMapper = new ObjectMapper();
King king = new King("Edward the Martyr","United Kingdom","House of Wessex","975-978");
objectMapper.writeValue(new File("target/king.json"), king);

Check fields which are null

It is possible to configure the Jackson ObjectMapper to fail if a JSON string contains a field with its value set to null:

objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);

In the above example, you will get an exception when trying to parse a null JSON field into a primitive Java field.

FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials