Support Online
Skip to main content

Using JSON-Simple in Java: A Guide to Reading and Writing JSON

🧠 What Will You Learn in This Guide?

In this guide, you'll learn how to create, write to, and read JSON data using Java's lightweight and standalone JSON library json-simple.
You will also see the basic usage of the JSONObject, JSONArray and JSONParser classes with practical examples.


🧩 1. What is json-simple?

json-simple is a small but effective library that is fully compliant with the JSON specification (RFC4627).
Ideal for reading, writing and editing JSON data without third-party dependencies.

Advantages:

  • Does not require external dependency.
  • Works with Map and List structures.
  • Can easily write or read JSON to file.
  • It is quite light and fast.

⚙️ 2. Adding json-simple Dependency with Maven

Add the following lines to the pom.xml file to include it in the project via Maven:

<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>

➡️ This dependency introduces the org.json.simple and org.json.simple.parser classes to your project.


✍️ 3. Writing JSON Data to File

In the example below, developer information is saved in the developer.json file using the JSONObject and JSONArray classes.


package com.genixnode.json.writer;

import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class JsonSimpleWriter {

@SuppressWarnings("unchecked")
public static void main(String[] args) {
JSONObject obj = new JSONObject();
obj.put("ad", "GenixNode Geliştirici");
obj.put("yas", 30);

JSONArray projeler = new JSONArray();
projeler.add("Bulut API");
projeler.add("Otonom Sunucu");
projeler.add("Veri Ambarı");

obj.put("projeler", projeler);

try (FileWriter file = new FileWriter("gelistirici.json")) {
file.write(obj.toJSONString());
System.out.println("JSON dosyası oluşturuldu: " + obj);
} catch (IOException e) {
e.printStackTrace();
}
}
}

➡️ This code writes the following content to the developer.json file:


{"projeler":["Bulut API","Otonom Sunucu","Veri Ambarı"],"ad":"GenixNode Geliştirici","yas":30}

📖 4. Reading JSON Data from File

The following example reads data from the same file and prints it to the screen.


package com.genixnode.json.reader;

import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JsonSimpleReader {

public static void main(String[] args) {
JSONParser parser = new JSONParser();

try (FileReader reader = new FileReader("gelistirici.json")) {
Object obj = parser.parse(reader);
JSONObject json = (JSONObject) obj;

String ad = (String) json.get("ad");
long yas = (Long) json.get("yas");
JSONArray projeler = (JSONArray) json.get("projeler");

System.out.println("Ad = " + ad);
System.out.println("Yaş = " + yas);

Iterator<String> it = projeler.iterator();
while (it.hasNext()) {
System.out.println("Proje = " + it.next());
}
} catch (IOException | ParseException e) {
e.printStackTrace();
}
}
}

➡️ Sample output:


Ad = GenixNode Geliştirici
Yaş = 30
Proje = Bulut API
Proje = Otonom Sunucu
Proje = Veri Ambarı

🧮 5. Things to Consider When Using json-simple

💡 Status🧾 Description
Map/List StructureJSON data naturally works with key-value and array logic.
Type Safety Warning@SuppressWarnings("unchecked") suppresses warnings caused by generic deficiency.
Data Type ConversionMethod JSONObject.get() returns Object, type conversion ((String), (Long)) is required.
Using UTF-8Recommended to avoid Turkish character problems.
Big Datajson-simple stores all data in memory; for large files use Jackson or Gson.

❓ Frequently Asked Questions (FAQ)

  1. Why does json-simple use Map and List?

Because the JSON structure consists of key-value pairs (Map) and arrays (List).

  1. What does toJSONString() do?

Converts a JSONObject or JSONArray object to text in JSON format.

  1. Is it enough for large JSON files?

json-simple is ideal for small and medium data. For big data, Jackson or Gson is recommended.

  1. Why is type conversion important when reading JSON?

get() method returns Object, it must be cast to be used in the correct type.

  1. Why is @SuppressWarnings("unchecked") necessary?

JSONObject does not support generic types; This annotation temporarily turns off IDE warnings.


🎯 Result

In this guide, you learned how to create and read JSON file using json-simple library. Thanks to its lightweight structure, it is a great option for managing JSON data in small projects.

💬 For more advanced JSON operations, you can test these examples on the virtual server you will install on the GenixNode platform and try them in your real-time applications immediately!