Main Tutorials

Convert JSON string to Map using Jackson

This article uses the Jackson framework to parse JSON strings to a Map in Java.

Table of contents:

P.S Tested with Jackson 2.17.0

1. Download Jackson

Simply declare jackson-databind in the pom.xml, and it will automatically pull in jackson-annotations, jackson-core, and other necessary dependencies.

pom.xml

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

2. Convert JSON string to Map

This example parses JSON string to a Map using Jackson.


    String json = "...";
    ObjectMapper mapper = new ObjectMapper();
    // convert JSON string to Map
    Map<String, Object> map = mapper.readValue(json, new TypeReference<>() {});
JsonToMapExample.java

package com.mkyong.json.jackson;

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

import java.util.Map;

public class JsonToMapExample {

    public static void main(String[] args) throws JsonProcessingException {

        // JSON string
        String json = "{\"name\": \"mkyong\", \"age\": 20}";

        // Create an ObjectMapper instance.
        ObjectMapper mapper = new ObjectMapper();

        // convert JSON string to Map
        // uncheck assignment
        // Map<String, Object> map = mapper.readValue(json, Map.class);

        // Convert JSON string to Map
        Map<String, Object> map = mapper.readValue(json, new TypeReference<>() {});

        // Output the contents of the Map to verify the conversion
        System.out.println("Map content: " + map);

    }

}

Output


{name=mkyong, age=37}

3. Convert Map to JSON string

This example converts a Java Map to a JSON string using Jackson.

MapToJsonExample.java

package com.mkyong.json.jackson;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;

public class MapToJsonExample {

    public static void main(String[] args) throws JsonProcessingException {

        ObjectMapper mapper = new ObjectMapper();

        Map<String, Object> map = new HashMap<>();
        map.put("name", "mkyong");
        map.put("age", 20);

        // convert map to JSON string
        String json = mapper.writeValueAsString(map);

        System.out.println(json);   // compact-print

        // enable pretty-print
        json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);

        System.out.println(json);   // pretty-print

    }

}

Output


{"name":"mkyong","age":20}
{
  "name" : "mkyong",
  "age" : 20
}

4. Download Source Code

5. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments