JSON (JavaScript Object Notation) is a lightweight, text-based, language-independent data exchange format. It is easy to read, write, and parse, making it widely used for data transfer between applications. JSON supports two main structured types:
- Object: An unordered collection of name/value pairs
- Array: An ordered list of values
Values can be strings, numbers, booleans, null, objects, or arrays.
Sample JSON Structure.
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
{ "type": "home", "number": "212 555-1234" },
{ "type": "fax", "number": "646 555-4567" }
]
}
JSON Processing in Java
Java does not have built-in JSON parsing support before Java 9, so external libraries are commonly used. In this article, we use JSON.simple, a lightweight and easy-to-use library.
JSON.simple Features:
- Parse JSON from strings or files.
- Create JSON objects and arrays.
- Simple API (JSONObject, JSONArray).
- Minimal dependencies.
Dependency Setup
Maven Dependency:
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
Core JSON.simple Classes
Class | Purpose |
|---|---|
JSONObject | Represents a JSON object (Map-like) |
JSONArray | Represents a JSON array (List-like) |
JSONParser | Parses JSON text into Java objects |
Write JSON to a file
This example creates a JSON object and writes it to a file using JSONObject and JSONArray.
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
public class JSONWriteExample {
public static void main(String[] args) throws Exception {
JSONObject jo = new JSONObject();
jo.put("firstName", "John");
jo.put("lastName", "Smith");
jo.put("age", 25);
Map<String, Object> address = new LinkedHashMap<>();
address.put("streetAddress", "21 2nd Street");
address.put("city", "New York");
address.put("state", "NY");
address.put("postalCode", 10021);
jo.put("address", address);
JSONArray phoneNumbers = new JSONArray();
Map<String, String> phone1 = new LinkedHashMap<>();
phone1.put("type", "home");
phone1.put("number", "212 555-1234");
Map<String, String> phone2 = new LinkedHashMap<>();
phone2.put("type", "fax");
phone2.put("number", "212 555-1234");
phoneNumbers.add(phone1);
phoneNumbers.add(phone2);
jo.put("phoneNumbers", phoneNumbers);
ObjectMapper mapper = new ObjectMapper();
ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
String prettyJson = writer.writeValueAsString(jo);
PrintWriter pw = new PrintWriter("JSONExample.json");
pw.write(prettyJson);
pw.close();
System.out.println(prettyJson);
}
}
Output (JSONExample.json)

Note : JSON objects are unordered by definition, so key order is not guaranteed.
Read JSON from a file
This example reads and parses the previously created JSON file.
import java.io.FileReader;
import java.util.Iterator;
import java.util.Map;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class JSONReadExample {
public static void main(String[] args) throws Exception {
Object obj = new JSONParser().parse(new FileReader("JSONExample.json"));
JSONObject jo = (JSONObject) obj;
String firstName = (String) jo.get("firstName");
String lastName = (String) jo.get("lastName");
long age = (long) jo.get("age");
System.out.println(firstName);
System.out.println(lastName);
System.out.println(age);
Map address = (Map) jo.get("address");
for (Object entry : address.entrySet()) {
Map.Entry pair = (Map.Entry) entry;
System.out.println(pair.getKey() + " : " + pair.getValue());
}
JSONArray phoneNumbers = (JSONArray) jo.get("phoneNumbers");
for (Object phoneObj : phoneNumbers) {
Map phone = (Map) phoneObj;
for (Object entry : phone.entrySet()) {
Map.Entry pair = (Map.Entry) entry;
System.out.println(pair.getKey() + " : " + pair.getValue());
}
}
}
}
Output:
