Limited Offer
60% OFF on all plans!

JSON in Java

Parse and serialize JSON using the Jackson library — ObjectMapper, @JsonProperty, handling nested objects, arrays, and unknown fields.

Introduction to JSON

JSON (JavaScript Object Notation) is a lightweight text format for data exchange. Java has no built-in JSON support — the most widely used library is **Jackson** (`com.fasterxml.jackson`). Add it via Maven or Gradle.

1. What is JSON?

JSON is a key-value text format. In Java, Jackson's `ObjectMapper` converts between JSON strings and Java objects.

JSON Data Types

Jackson maps JSON types to Java types automatically. Understanding the mapping prevents type mismatch errors during deserialization.

1. Supported JSON Data Types

Each JSON type maps to a corresponding Java type when deserialized by Jackson.

JSON Syntax Rules

Jackson is strict about JSON syntax. Invalid JSON throws a `JsonParseException`. Knowing the rules prevents common parsing errors.

1. Important JSON Rules

JSON requires double-quoted keys, no trailing commas, and no comments.

Converting Object to JSON

`ObjectMapper.writeValueAsString()` converts a Java object to a JSON string. Use `writerWithDefaultPrettyPrinter()` for formatted output.

1. Basic Serialization

Use `writeValueAsString()` to convert a Java object or Map to a JSON string.

2. Pretty Printing JSON

Use `writerWithDefaultPrettyPrinter()` to produce human-readable indented JSON.

3. Serializing a POJO

Jackson can serialize any plain Java class (POJO) — fields are mapped to JSON keys automatically.

Converting JSON to Object

`ObjectMapper.readValue(json, Type.class)` converts a JSON string to a Java object. The target class must have a no-arg constructor (or use `@JsonCreator`).

1. Basic Deserialization

Use `readValue(json, ClassName.class)` to parse JSON into a typed Java object.

2. Nested Objects & Arrays

Jackson handles nested objects and arrays automatically — just define matching fields in your class.

3. Parsing to Basic Types (List, Map, String, Number)

Use `TypeReference` to parse JSON directly into `List`, `Map`, or primitive types — no custom class needed.

Handling Invalid JSON

Jackson throws `JsonParseException` for invalid JSON syntax and `JsonMappingException` for type mismatches. Always wrap parsing in try-catch for user-supplied input.

1. Safe JSON Parsing

Wrap `readValue()` in a try-catch to handle invalid JSON gracefully.

Handling Unknown Fields

By default, Jackson throws an error if the JSON contains a field that doesn't exist in the target class. Use `@JsonIgnoreProperties` or `DeserializationFeature` to ignore unknown fields.

1. Ignoring Unknown Fields

Use `@JsonIgnoreProperties(ignoreUnknown = true)` on the class or configure the mapper globally.

@JsonProperty — Custom Field Names

Use `@JsonProperty("key")` when the JSON key name differs from your Java field name — common when working with snake_case APIs.

1. Mapping Snake_case JSON to camelCase Java

`@JsonProperty` maps a specific JSON key to a Java field with a different name.

Deep Cloning Objects

A quick way to deep clone a Java object is to serialize it to JSON and deserialize it back. This creates a completely independent copy with no shared references.

1. Deep Clone using Jackson

Serialize to JSON string then deserialize back — produces a deep copy with no shared references.