Categories: Json

Converting JSON to/from Java Maps using JACKSON API

Since JSON format is essentially a key-value pair grouping, JSON can easily be converted to/from Java maps. This post is about JSON to MAP conversion. Let’s get going.

Step 1: Include JACKSON dependency in pom.xml

There are several jar in JACKSON library. For this example, we are only using databind.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.websystique.xml</groupId>
  <artifactId>JsonJacksonMapMappingDemo</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>

  <name>JsonJacksonMapMappingDemo</name>

  <dependencies>
 <dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.5.3</version>
 </dependency>
  </dependencies>
 </project>

Step 2: Convert Java Map to JSON and write JSON to a file

Populate a Map, convert into JSON and write that JSON to a file

package com.websystique.json.jackson;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonJacksonMapMappingDemo {

 public static void main(String args[]) {
  ObjectMapper mapper = new ObjectMapper();

  Map<String, Object> carMap = new HashMap<String, Object>();

  carMap.put("car", "Audi");
  carMap.put("price", "30000");
  carMap.put("model", "2010");

  List<String> colors = new ArrayList<String>();
  colors.add("Grey");
  colors.add("White");
  colors.add("Black");

  carMap.put("colors", colors);

  /**
   * Convert Map to JSON and write to a file
   */  try {
   mapper.writeValue(new File("result.json"), carMap);
  } catch (Exception e) {
   e.printStackTrace();
  }

 }
}

Now open result.json, you should see following output

{"car":"Audi","model":"2010","price":"30000","colors":["Grey","White","Black"]}

Step 3: Convert JSON to Java Map

Read JSON string from a file and convert JSON to Java Map

package com.websystique.json.jackson;

import java.io.File;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonJacksonMapMappingDemo {

 public static void main(String args[]) {
  ObjectMapper mapper = new ObjectMapper();


  /**
   * Read JSON from a file into a Map
   */  try {
   Map<String, Object> carMap = mapper.readValue(new File(
     "result.json"), new TypeReference<Map<String, Object>>() {
   });

   System.out.println("Car : " + carMap.get("car"));
   System.out.println("Price : " + carMap.get("price"));
   System.out.println("Model : " + carMap.get("model"));
   System.out.println("Colors : " + carMap.get("colors"));

  } catch (Exception e) {
   e.printStackTrace();
  }

 }
}

Below is the output:

Car : Audi
Price : 30000
Model : 2010
Colors : [Grey, White, Black]

That’s it. In the next post we will learn about Jackson Tree model api.

References

View Comments

Share
Published by

Recent Posts

Spring Boot + AngularJS + Spring Data + JPA CRUD App Example

In this post we will be developing a full-blown CRUD application using Spring Boot, AngularJS, Spring Data, JPA/Hibernate and MySQL,…

7 years ago

Spring Boot Rest API Example

Spring Boot complements Spring REST support by providing default dependencies/converters out of the box. Writing RESTful services in Spring Boot…

7 years ago

Spring Boot WAR deployment example

Being able to start the application as standalone jar is great, but sometimes it might not be possible to run…

7 years ago

Spring Boot Introduction + hello world example

Spring framework has taken the software development industry by storm. Dependency Injection, rock solid MVC framework, Transaction management, messaging support,…

7 years ago

Secure Spring REST API using OAuth2

Let's secure our Spring REST API using OAuth2 this time, a simple guide showing what is required to secure a…

8 years ago

AngularJS+Spring Security using Basic Authentication

This post shows how an AngularJS application can consume a REST API which is secured with Basic authentication using Spring…

8 years ago