jsondecode.com logo

JSON in Java — Jackson, Gson & org.json

Java does not have built-in JSON support, but three libraries dominate: Jackson (most popular in enterprise), Gson (from Google, simple API), and org.json (lightweight, no dependencies).

Jackson — parse JSON into a POJO

Jackson's ObjectMapper is the standard choice for enterprise Java. Add com.fasterxml.jackson.core:jackson-databind to your build.

import com.fasterxml.jackson.databind.ObjectMapper;

public class User {
    public int id;
    public String name;
    public String email;
}

ObjectMapper mapper = new ObjectMapper();
String json = "{"id":1,"name":"Alice","email":"alice@example.com"}";
User user = mapper.readValue(json, User.class);
System.out.println(user.name); // Alice

Jackson — serialize to JSON

Use mapper.writeValueAsString() to serialize a POJO to a JSON string.

User user = new User();
user.id = 1;
user.name = "Alice";

String json = mapper.writeValueAsString(user);
// {"id":1,"name":"Alice","email":null}

// Pretty-print
String pretty = mapper
    .writerWithDefaultPrettyPrinter()
    .writeValueAsString(user);

Gson — parse and serialize

Gson is simpler for small projects. Add com.google.code.gson:gson to your build.

import com.google.gson.Gson;

Gson gson = new Gson();

// Parse
User user = gson.fromJson(json, User.class);

// Serialize
String output = gson.toJson(user);

Handle JSON parse errors in Java

Jackson throws JsonProcessingException (a checked exception) on parse failure. Always handle it.

import com.fasterxml.jackson.core.JsonProcessingException;

try {
    User user = mapper.readValue("{invalid}", User.class);
} catch (JsonProcessingException e) {
    System.err.println("Parse error: " + e.getOriginalMessage());
}

Frequently Asked Questions

What is the best JSON library for Java?

Jackson (jackson-databind) is the most widely used in enterprise Java. Gson is simpler for smaller projects. For Spring Boot, Jackson is included by default.

How do I parse JSON in Java with Jackson?

Create an ObjectMapper and call mapper.readValue(jsonString, MyClass.class). Your class needs a no-arg constructor or Jackson annotations.

How do I ignore unknown JSON properties in Jackson?

Add @JsonIgnoreProperties(ignoreUnknown = true) to your class, or configure the ObjectMapper globally: mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).

How do I serialize null fields in Jackson?

By default, null fields are included. To skip them globally: mapper.setSerializationInclusion(Include.NON_NULL). Or use @JsonInclude(Include.NON_NULL) on the class.

What is the difference between Jackson and Gson?

Jackson is faster and has more features (streaming API, tree model, extensive annotations). Gson is simpler to use with minimal configuration. For most Java/Spring projects, Jackson is the better choice.

Format and validate your JSON instantly

Free, no ads, no sign-up. Also converts JSON to TypeScript, YAML, CSV, and more.

Open JSON Formatter →

If jsondecode.com saved you time, share it with your team

Free forever. No ads. No sign-up. Help other developers find it.