The org.json library is easy to use.

Just remember (while casting or using methods like getJSONObject and getJSONArray) that in JSON notation

  • [ … ] represents an array, so library will parse it to JSONArray
  • { … } represents an object, so library will parse it to JSONObject

Example code below:

import org.json.*;

String jsonString = ... ; //assign your JSON String here
JSONObject obj = new JSONObject(jsonString);
String pageName = obj.getJSONObject("pageInfo").getString("pageName");

JSONArray arr = obj.getJSONArray("posts"); // notice that `"posts": [...]`
for (int i = 0; i < arr.length(); i++)
{
    String post_id = arr.getJSONObject(i).getString("post_id");
    ......
}

You may find more examples from: Parse JSON in Java

Downloadable jar: http://mvnrepository.com/artifact/org.json/json

Answer from user1931858 on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › working-with-json-data-in-java
Working with JSON Data in Java - GeeksforGeeks
December 23, 2025 - Java Tutorial · Advanced Java ... · Last Updated : 23 Dec, 2025 · JSON (JavaScript Object Notation) is a lightweight, text-based, language-independent data format used for data exchange....
🌐
Oracle
docs.oracle.com › javaee › 7 › tutorial › jsonp001.htm
19.1 Introduction to JSON - Java Platform, Enterprise Edition: The Java EE Tutorial (Release 7)
Java Platform, Enterprise Edition: The Java EE Tutorial · Previous PageNext Page · JSON is a text-based data exchange format derived from JavaScript that is used in web services and other connected applications. The following sections provide an introduction to JSON syntax, an overview of JSON uses, and a description of the most common approaches to generate and parse JSON.
Discussions

Generate example JSON from Java classes
Jackson? It is the built in for SpringBoot. https://www.baeldung.com/jackson-object-mapper-tutorial More on reddit.com
🌐 r/learnjava
9
5
February 8, 2023
How to deserialize Json to java enum using Jackson ?
Jackson serialises between Strings in JSON and enums in Java out of the box. I doubt that's the problem. For example I don't see a attendanceJson field on your class. Can you post the full code? This works on my machine: import com.fasterxml.jackson.databind.ObjectMapper; public class JacksonEnum { public static void main(String... argv) throws Exception { ObjectMapper mapper = new ObjectMapper(); MyDto dto = mapper.readValue("{\"examType\":\"SECOND_TERM\"}", MyDto.class); System.out.println(dto.getExamType()); } public static class MyDto { public ExamType getExamType() { return examType; } public void setExamType(ExamType examType) { this.examType = examType; } private ExamType examType; } public enum ExamType { FIRST_TERM, SECOND_TERM, FINAL, QUIZ_1, QUIZ_2, QUIZ_3, QUIZ_4, QUIZ_5, TUTORIAL_1, TUTORIAL_2, TUTORIAL_3, TUTORIAL_4, TUTORIAL_5; } } Using jackson-databind:2.8.6 More on reddit.com
🌐 r/javahelp
15
4
February 18, 2017
🌐
TutorialsPoint
tutorialspoint.com › home › json › json example in java
JSON Example in Java
March 14, 2013 - Discover how to handle JSON in Java with a detailed example on parsing and object mapping.
🌐
W3Schools
w3schools.com › js › js_json.asp
W3Schools.com
You can work with data as JavaScript objects, with no complicated parsing and translations. When storing data, the data has to be a certain format, and regardless of where you choose to store it, text is always one of the legal formats.
Top answer
1 of 16
962

The org.json library is easy to use.

Just remember (while casting or using methods like getJSONObject and getJSONArray) that in JSON notation

  • [ … ] represents an array, so library will parse it to JSONArray
  • { … } represents an object, so library will parse it to JSONObject

Example code below:

import org.json.*;

String jsonString = ... ; //assign your JSON String here
JSONObject obj = new JSONObject(jsonString);
String pageName = obj.getJSONObject("pageInfo").getString("pageName");

JSONArray arr = obj.getJSONArray("posts"); // notice that `"posts": [...]`
for (int i = 0; i < arr.length(); i++)
{
    String post_id = arr.getJSONObject(i).getString("post_id");
    ......
}

You may find more examples from: Parse JSON in Java

Downloadable jar: http://mvnrepository.com/artifact/org.json/json

2 of 16
714

For the sake of the example lets assume you have a class Person with just a name.

private class Person {
    public String name;

    public Person(String name) {
        this.name = name;
    }
}

Jackson (Maven)

My personal favourite and probably the most widely used.

ObjectMapper mapper = new ObjectMapper();

// De-serialize to an object
Person user = mapper.readValue("{\"name\": \"John\"}", Person.class);
System.out.println(user.name); //John

// Read a single attribute
JsonNode nameNode = mapper.readTree("{\"name\": \"John\"}");
System.out.println(nameNode.get("name").asText());

Google GSON (Maven)

Gson g = new Gson();

// De-serialize to an object
Person person = g.fromJson("{\"name\": \"John\"}", Person.class);
System.out.println(person.name); //John

// Read a single attribute
JsonObject jsonObject = new JsonParser().parseString("{\"name\": \"John\"}").getAsJsonObject();
System.out.println(jsonObject.get("name").getAsString()); //John

Org.JSON (Maven)

This suggestion is listed here simply because it appears to be quite popular due to stackoverflow reference to it. I would not recommend using it as it is more a proof-of-concept project than an actual library.

JSONObject obj = new JSONObject("{\"name\": \"John\"}");

System.out.println(obj.getString("name")); //John
🌐
Mkyong
mkyong.com › home › tutorials › java json tutorials
Java JSON Tutorials - Mkyong.com
May 17, 2024 - List of Java JSON tutorials, examples and source code for study.
Find elsewhere
🌐
CodeSignal
codesignal.com › learn › courses › handling-json-files-with-java › lessons › creating-and-writing-json-data-with-java-using-jackson
Creating and Writing JSON Data with Java
In this lesson, you've gained skills in constructing and writing JSON data using Java. We began with simple objects, expanded into complex structures involving collections, and wrote the data to a file in a clearly formatted manner.
🌐
INNOQ
innoq.com › en › articles › 2022 › 02 › java-json
Processing JSON in Java
February 22, 2022 - In order to work in code with a JSONObject or JSONArray, a range of methods are available to us. For example, we can use has or isNull to check whether a field exists and is not null. Although isNull also returns true for fields that do not exist. In order to query individual field values, we can choose from an array of getXxx methods that return the value in the required Java data type.
🌐
Baeldung
baeldung.com › home › json › introduction to json-java (org.json)
Introduction to JSON-Java | Baeldung
June 20, 2025 - In this tutorial, we’ll see how to create, manipulate, and parse JSON using one of the available JSON processing libraries in Java – the JSON-Java library, also known as org.json.
🌐
YouTube
youtube.com › watch
A quick tutorial on using JSON-simple parsing JSON ...
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-json-example
Java JSON Example | DigitalOcean
August 4, 2022 - Welcome to the Java JSON Example Tutorial. JSON (JavaScript Object Notation) is text-based lightweight technology for generating human readable formatted data. JSON represent object data in the form of key-value pairs.
🌐
Attacomsian
attacomsian.com › blog › java-read-write-json-files
How to read and write JSON Files in Java
October 3, 2022 - Learn how to read and write JSON files using JSON.simple, Jackson, Gson, and Mushi open-source libraries.
🌐
Reddit
reddit.com › r/learnjava › generate example json from java classes
r/learnjava on Reddit: Generate example JSON from Java classes
February 8, 2023 -

TL;DR: Looking for a tool that can generate example-json from POJOs

We use an event-driven architecture and dispatch the event-payloads as json-strings. We also have to document the structure of these payloads and currently we do this by typing json-examples manually.

The payloads are basically just POJOs with a constructor, getters and setters.

I'm looking for a tool to streamline the documentation-process! Is there a software that can generate example json from Java classes?

I'm really thankful for any recommendation!

🌐
JAXB
javaee.github.io › tutorial › jsonp001.html
Introduction to JSON
The following example shows JSON data for a sample object that contains name-value pairs. The value for the name "phoneNumbers" is an array whose elements are two objects. { "firstName": "Duke", "lastName": "Java", "age": 18, "streetAddress": "100 Internet Dr", "city": "JavaTown", "state": "JA", "postalCode": "12345", "phoneNumbers": [ { "Mobile": "111-111-1111" }, { "Home": "222-222-2222" } ] }
🌐
Jakarta EE
jakarta.ee › learn › docs › jakartaee-tutorial › current › web › jsonp › jsonp.html
JSON Processing :: Jakarta EE Tutorial :: Jakarta EE Documentation
The jakarta.json package contains a reader interface, a writer interface, a model builder interface for the object model, and utility classes and Java types for JSON elements. This package also includes several classes that implement other JSON-related standards: JSON Pointer, JSON Patch, and ...
🌐
Medium
medium.com › @Mohd_Aamir_17 › mastering-json-in-java-a-comprehensive-guide-to-handling-json-objects-arrays-and-nodes-with-df57bf0ebff1
Mastering JSON in Java: A Comprehensive Guide to Handling JSON Objects, Arrays, and Nodes with Jackson | by Mohd Aamir | Medium
November 4, 2024 - This article offers an in-depth look at how to parse, create, and manipulate JSON objects, arrays, and nodes using the Jackson library, covering advanced scenarios to equip you with the tools to tackle complex JSON data in Java.
🌐
JSON
json.org
JSON
A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used.
🌐
Oracle
oracle.com › java › technical details
Java API for JSON Processing
Listing 2. JSON representation of searching Facebook public posts · We can use the object model API to get names and their public posts about the term java.
🌐
Baeldung
baeldung.com › home › json › jackson › json in java
JSON in Java | Baeldung
May 11, 2024 - Learn how to start working with JSON data in Java.
🌐
Reintech
reintech.io › blog › java-json-processing-working-with-json-data-in-java
Java JSON processing: Working with JSON data in Java | Reintech media
April 18, 2023 - Learn how to work with JSON data in Java using JSON.simple, Gson, and Jackson libraries. This comprehensive tutorial covers parsing, creating, and manipulating JSON data in Java.