How to Convert a Java Map to JSON

Java Map output and JSON can look similar, but they are different formats. A Map printed in a console might look like this:

{name=kim, age=30, active=true}

The corresponding JSON uses quoted keys and colons:

{
  "name": "kim",
  "age": 30,
  "active": true
}

First, check what you have. If you still have the actual Java Map object, serialize it with Jackson or Gson. If you only have Map.toString() output copied from a log or console, you need to interpret that text before you can create a JSON object. These are different operations.

Java Map vs JSON

A Java Map is an object containing keys and values. Its toString() output is only a text representation. JSON is a defined data interchange format with syntax for objects, arrays, strings, numbers, booleans, and null.

Map.toString() output

{name=kim, age=30}

JSON

{
  "name": "kim",
  "age": 30
}

Map output normally uses equals signs; JSON uses colons. JSON object keys and string values require double quotes. Blindly replacing = with : leaves keys unquoted and does not handle escaping or value types, so it is not a safe conversion.

Convert a Java Map to JSON with Jackson

Pass the Map object to ObjectMapper. This complete example targets Jackson 2.x and requires the com.fasterxml.jackson.core:jackson-databind dependency in your Java project:

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

import java.util.LinkedHashMap;
import java.util.Map;

public class MapToJson {
    public static void main(String[] args) throws JsonProcessingException {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("name", "kim");
        map.put("age", 30);
        map.put("active", true);

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(map);

        System.out.println(json);
    }
}

Output

{"name":"kim","age":30,"active":true}

For readable indentation, replace the serialization line with:

String json = mapper.writerWithDefaultPrettyPrinter()
    .writeValueAsString(map);

The example declares the checked exception; application code can catch it where serialization failures should be handled. See the ObjectMapper API documentation for these methods.

Spring Boot projects often already use Jackson, but check your actual dependencies and major version. Do not assume every project includes it. Spring Boot 4 defaults to Jackson 3, whose imports differ from this Jackson 2 example; consult your version's Spring Boot JSON documentation.

Convert a Java Map to JSON with Gson

Gson is another option when you have the original object. It requires the com.google.code.gson:gson dependency. In the previous example, keep the Map setup, add import com.google.gson.Gson;, and replace the Jackson serialization statements with:

Gson gson = new Gson();
String json = gson.toJson(map);
System.out.println(json);

For pretty output, also import com.google.gson.GsonBuilder and use this version instead:

Gson gson = new GsonBuilder()
    .setPrettyPrinting()
    .create();

String json = gson.toJson(map);
System.out.println(json);

Remove the Jackson imports and exception declaration if you switch the example entirely to Gson. Both versions produce JSON from the Map; pretty printing changes its presentation. Gson normally omits null object fields; configure serializeNulls() on the builder if you need them. The Gson User Guide covers serialization options.

Nested Maps and Lists

For string-keyed Maps containing ordinary supported values, nested Maps become JSON objects and Lists become JSON arrays. Replace the Map setup in the Jackson example with the following, and add import java.util.Arrays;. Arrays.asList() also works on Java 8.

Map<String, Object> address = new LinkedHashMap<>();
address.put("city", "Seoul");
address.put("zip", "12345");

Map<String, Object> map = new LinkedHashMap<>();
map.put("name", "kim");
map.put("address", address);
map.put("skills", Arrays.asList("Java", "Spring"));

Jackson or Gson can serialize this structure. With indentation, the JSON is equivalent to:

{
  "name": "kim",
  "address": {
    "city": "Seoul",
    "zip": "12345"
  },
  "skills": [
    "Java",
    "Spring"
  ]
}

The ZIP code stays a string because its Java value is a String. The serializer works from the object's values, rather than guessing their types from printed text.

What if you only have Map.toString() output?

A log may contain only this text:

{name=kim, age=30, active=true, address={city=Seoul}}

That is a String, not a Map object. Passing it to a serializer does not parse its entries. For example, with the ObjectMapper above:

String text = "{name=kim, age=30}";
System.out.println(mapper.writeValueAsString(text));

The result is one JSON string, including its surrounding quotes:

"{name=kim, age=30}"

It does not become a JSON object with name and age properties. If the original Map is unavailable and the text follows the supported Map-style syntax, the Java Map to JSON Converter can help reconstruct the structure.

Convert Map.toString() output online

Open the Java Map to JSON Converter, paste the text into Input, choose 2 or 4 spaces, and press Convert. Processing runs locally in your browser without uploading the input.

The converter supports Maps and Lists, including nested Maps, nested Lists, Maps inside Lists, and Lists inside Maps. Lowercase true, false, and null become the corresponding JSON values. Supported integers and decimals become numbers; other scalar values remain strings.

Input

{name=kim, age=30, active=true, skills=[Java, Spring]}

Output

{
  "name": "kim",
  "age": 30,
  "active": true,
  "skills": [
    "Java",
    "Spring"
  ]
}

Important type limitations

Map.toString() can lose type information. A Java String containing "true" and a Boolean value of true can both appear as active=true in printed Map output. The online converter infers a boolean from that text; it cannot know which Java type produced it.

Prefer Jackson or Gson whenever you have the original Map object. Reconstruction from a log string is useful when that object is gone, but inferred types are not a guarantee of the original data types.

Common mistakes

Replacing = with :

A global replacement does not quote or escape keys and strings, parse nested structures, or distinguish text from numbers and booleans. It can also change an equals sign that belongs inside a value. JSON conversion requires understanding the structure and values.

Passing Map.toString() directly to JSON.parse

JavaScript's JSON.parse() expects valid JSON. Text such as {name=kim, age=30} has unquoted keys and equals signs, so parsing it throws a syntax error.

Serializing the Map string instead of the Map object

Pass map, not map.toString(), to ObjectMapper when you need a JSON object. A String argument produces a JSON string, even if its contents look like a collection of key-value pairs.

Which approach should you use?

Choose based on the data you have
SituationRecommended approach
Have the actual Java Map objectJackson or Gson
Using Spring/Jackson alreadyYour configured ObjectMapper
Using Gson alreadyGson.toJson()
Only have Map.toString() outputJava Map to JSON Converter for supported text
Need only readable Map syntaxJava Map Pretty Formatter

Current online converter limitations

  • Commas inside quoted strings are not fully supported. Input such as {name="Kim, Lee"} can fail because the comma is treated as an item separator.
  • DTO toString formats such as User(name=kim) are not supported.
  • Duplicate Map keys keep the last JSON value.
  • Numbers follow JavaScript number precision limits. Very large integers may lose precision.
  • Numeric inference accepts integers and decimals with an optional minus sign, no extra leading zeros, and a finite JavaScript result. Values such as 00123, 1e3, and +10 remain strings. So do uppercase TRUE and NULL.