Overview
JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format widely used for configuration files, REST APIs, and inter-application communication. Its simplicity, readability, and language-agnostic structure make it a de facto standard for transmitting structured data between systems.
This page explains the fundamentals of JSON, how it represents data, and how Java applications parse and generate JSON using common libraries.
JSON Structure and Syntax
JSON defines two primary data structures:
- Objects: Unordered sets of name/value pairs, enclosed in curly braces
{} - Arrays: Ordered collections of values, enclosed in square brackets
[]
JSON supports the following data types:
- Strings (e.g.,
"name": "Alice") - Numbers (e.g.,
"age": 31) - Booleans (
true,false) - Null (
null) - Objects and arrays
Example JSON object:
{
"name": "Alice",
"age": 31,
"isEmployee": true,
"skills": ["Java", "JSON", "REST"],
"address": {
"city": "Delhi",
"country": "India"
}
}
This JSON object includes nested objects and arrays, demonstrating common data patterns.
Why JSON Is Used
JSON’s strengths include:
- Human readability — easy to inspect and edit
- Lightweight format — minimal syntax overhead
- Language independence — universally supported across platforms
- Interoperability with web APIs — standard for HTTP data exchange
In Java applications, JSON often appears in:
- REST API responses and requests
- Configuration files
- Data serialization between components
Parsing and Generating JSON in Java
Java does not include a native JSON parser in the standard library, but there are widely adopted libraries that provide robust support. Two common options are:
- org.json (lightweight, simple parsing)
- Gson (from Google, flexible serialization/deserialization)
Using org.json for JSON Handling
To work with JSON using org.json, you may add the library to your project.
Parsing a JSON String
import org.json.JSONObject;
import org.json.JSONArray;
public class JsonExample {
public static void main(String[] args) {
String jsonString = "{ \"name\":\"Alice\",\"age\":31 }";
JSONObject jsonObject = new JSONObject(jsonString);
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
Generating JSON
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "Alice");
jsonObject.put("age", 31);
System.out.println(jsonObject.toString());
org.json provides intuitive methods to read and write JSON data.
Using Gson for Java JSON Mapping
Gson is a popular JSON library that supports serialization (Java object → JSON) and deserialization (JSON → Java object) with minimal code.
Serializing Java Objects
import com.google.gson.Gson;
public class GsonExample {
public static void main(String[] args) {
Gson gson = new Gson();
Person person = new Person("Alice", 31);
String json = gson.toJson(person);
System.out.println(json);
}
}
class Person {
private String name;
private int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
}
Deserializing JSON to Java Object
String jsonString = "{\"name\":\"Alice\",\"age\":31}";
Person person = gson.fromJson(jsonString, Person.class);
System.out.println(person.getName());
Gson simplifies JSON mapping with type safety and custom configurations when needed.
Choosing the Right JSON Library
| Library | Best For |
|---|---|
org.json | Simple use cases, minimal dependencies |
| Gson | Full object mapping, flexible serialization |
| Jackson | High performance, enterprise-level features |
When selecting a library, consider:
- Speed and performance requirements
- Need for object mapping vs simple parsing
- Project dependency standards
Common JSON Use Cases in Java
JSON is frequently used in Java for:
- REST API interaction (client/server)
- Microservice communication
- Configuration and logging data
- Data serialization for storage or transfer
Understanding how to work with JSON efficiently is essential for modern backend and mobile API integration.
JSON Best Practices
- Validate JSON structure before parsing
- Handle exceptions for missing or malformed fields
- Use objects and arrays judiciously to model complex data
- Avoid deeply nested JSON when simpler structures suffice


