Programming & Development / April 19, 2025

How to Convert a JSON String to Map in Java

Java JSON Map String Jackson Gson ObjectMapper JSON to Map JSON parsing Spring Boot

Converting JSON to Map<String, String> in Java

Use case: You receive a JSON string like {"Key":"value"} (perhaps from an API or file), and you want to convert it into a Java Map<String, String> for easier manipulation.

✅ Using Jackson

1. Add Jackson Dependency (if using Maven)

xml

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.16.0</version> <!-- Use latest version -->
</dependency>

2. Sample Code

java

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

import java.util.Map;

public class JacksonJsonToMap {
    public static void main(String[] args) throws Exception {
        String json = "{\"Key\":\"value\"}";

        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, String> map = objectMapper.readValue(json, new TypeReference<Map<String, String>>() {});

        System.out.println(map); // Output: {Key=value}
    }
}

🔍 Explanation:

  • ObjectMapper is the main class in Jackson for JSON parsing.
  • TypeReference<Map<String, String>>() {} is used to ensure proper typing and avoid raw type warnings.

✅ Using Gson

1. Add Gson Dependency (if using Maven)

xml

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.10.1</version> <!-- Use latest version -->
</dependency>

2. Sample Code

java

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Map;

public class GsonJsonToMap {
    public static void main(String[] args) {
        String json = "{\"Key\":\"value\"}";

        Gson gson = new Gson();
        Type type = new TypeToken<Map<String, String>>(){}.getType();
        Map<String, String> map = gson.fromJson(json, type);

        System.out.println(map); // Output: {Key=value}
    }
}

🔍 Explanation:

  • Gson provides a simple way to deserialize a JSON string.
  • TypeToken<Map<String, String>>(){}.getType() ensures proper type handling at runtime.

⚖️ Comparison: Jackson vs Gson

FeatureJacksonGsonPerformanceFaster for large or complex JSONSlightly slowerConfigurationHighly customizableSimpler, less configuration neededTypingStrong typing via TypeReferenceUses TypeToken for genericsPopularityWidely used in enterprise applicationsCommon in Android and lightweight apps

✅ Output (Both Methods)

plaintext

{Key=value}

✅ Use in Spring Boot

In Spring Boot apps, Jackson is included by default, so if you're using Spring, you likely don’t need to add extra dependencies—just inject and use ObjectMapper.


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex