Easy step to convert JSON into Java Object
By: Teten Nugraha
Tags: Rest, Programming, Java, Json, API

Easy step to convert JSON into Java Object Teten Nugraha Follow Sep 2 · 3 min read

Introduction

In this era of all-round use of the internet, the need for data traffic is also very fast. One of the APIs that are widely used today is REST by using the json message format, so on the development side it will also be very easy to use. Java is a static language different from scripting languages such as PHP, Javascript which can easily transform json messages, but Java itself needs a special trick so that these requirements are met.

Prerequisites

JDK 1.8 or up Maven your favorite IDE

How to use it ?

Create a maven project and add this dependency or library

<dependency>

<groupId>com.fasterxml.jackson.core</groupId>

<artifactId>jackson-databind</artifactId>

<version>2.12.4</version>

</dependency>

You can get it with this link https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.12.4

Here is the json file that we will use, in this case we will transform the json person into a person object

Json Object

json array

From the json object above, we can see that we need the name, city and email attributes for that to create an entity class in java code with the name Person as below.

public class Person { private String nama;

private String kota;

private String email; public Person() {

} public String getNama() {

return nama;

} public void setNama(String nama) {

this.nama = nama;

} public String getKota() {

return kota;

} public void setKota(String kota) {

this.kota = kota;

} public String getEmail() {

return email;

} public void setEmail(String email) {

this.email = email;

} @Override

public String toString() {

return "Person{" +

"nama='" + nama + '\'' +

", kota='" + kota + '\'' +

", email='" + email + '\'' +

'}';

}

} public String toString() {return "Person{" +"nama='" + nama + '\'' +", kota='" + kota + '\'' +", email='" + email + '\'' +'}';

with this method, we can transform json into java object

private static List<Person> transformToObjList(String personStrList) throws JsonProcessingException {

return Arrays.asList(objectMapper.readValue(personStrList, Person[].class));

} private static Person transformToObj(String personStr) throws JsonProcessingException {

return objectMapper.readValue(personStr, Person.class);

}

here is an example I made in my IDE,

package id.ten.latihan; import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Arrays;

import java.util.List; public class ProcessingJson { final static ObjectMapper objectMapper = new ObjectMapper(); public static void main(String[] args) {

" \"nama\":\"Teten Nugraha\",

" +

" \"kota\":\"Bandung\",

" +

" \"email\":\"

"}"; final String personStr = "{

" +" \"nama\":\"Teten Nugraha\",

" +" \"kota\":\"Bandung\",

" +" \"email\":\" teten@mail.com \"

" +"}";

" {

" +

" \"nama\":\"Teten Nugraha\",

" +

" \"kota\":\"Bandung\",

" +

" \"email\":\"

" },

" +

" {

" +

" \"nama\":\"Budi Gunawan\",

" +

" \"kota\":\"Garut\",

" +

" \"email\":\"

" },

" +

" {

" +

" \"nama\":\"Agus Cahya\",

" +

" \"kota\":\"Tasik\",

" +

" \"email\":\"

" }

" +

"]"; final String personStrList = "[

" +" {

" +" \"nama\":\"Teten Nugraha\",

" +" \"kota\":\"Bandung\",

" +" \"email\":\" teten@mail.com \"

" +" },

" +" {

" +" \"nama\":\"Budi Gunawan\",

" +" \"kota\":\"Garut\",

" +" \"email\":\" budi@mail.com \"

" +" },

" +" {

" +" \"nama\":\"Agus Cahya\",

" +" \"kota\":\"Tasik\",

" +" \"email\":\" agus@mail.com \"

" +" }

" +"]"; try{

final Person person = transformToObj(personStr);

final List<Person> personList = transformToObjList(personStrList); System.out.println("Get One Object");

System.out.println(person.toString()+"



"); System.out.println("Get form list");

personList.forEach(val -> System.out.println(val)); }catch (JsonProcessingException e) {

System.out.println("failed when transforming json");

} } private static List<Person> transformToObjList(String personStrList) throws JsonProcessingException {

return Arrays.asList(objectMapper.readValue(personStrList, Person[].class));

} private static Person transformToObj(String personStr) throws JsonProcessingException {

return objectMapper.readValue(personStr, Person.class);

} }

if we run, then the result is as follows,

Conclusion

As we can see, using this library we can easily convert json files into Java objects. Besides ObjectMapper, we can also use Gson.