org.json.JSONArray may be what you want.

String message;
JSONObject json = new JSONObject();
json.put("name", "student");

JSONArray array = new JSONArray();
JSONObject item = new JSONObject();
item.put("information", "test");
item.put("id", 3);
item.put("name", "course1");
array.put(item);

json.put("course", array);

message = json.toString();

// message
// {"course":[{"id":3,"information":"test","name":"course1"}],"name":"student"}
Answer from srain on Stack Overflow
🌐
Baeldung
baeldung.com › home › json › introduction to json-java (org.json)
Introduction to JSON-Java | Baeldung
June 20, 2025 - In the example below, the package uses the getters from the DemoBean class and creates an appropriate JSONObject for the same. To get a JSONObject from a Java Object, we’ll have to use a class that is a valid Java Bean:
Discussions

Creating JSON object in JAVA client to post into REST API
Hi, I am writing a java client that makes REST calls to Camunda. I understand the following code should allow me to create JSON string that is correctly formatted so that it can be posted into the API ( as per the variable format we see here: https://docs.camunda.org/manual/7.4/reference/r... More on forum.camunda.io
🌐 forum.camunda.io
0
0
April 22, 2016
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
Convert Java object to JSON without external library
Converting arbitrary objects to JSON isn't particularly easy, especially when there are full solutions already available. Personally, I use Gson, which is pretty easy to use. If the no external libraries is a HARD requirement, then your only choice would be to use reflection to get the names and values of the fields of the object to build the JSON string. You might have recursion problems if there are nested objects that create a circular loop, which complicates things. If you only have to serialize specific classes, then it would probably be easier to essentially hardcode the JSON string generation, and just call the appropriately getters to build the string. Basically, if it's only a few specific classes then you can write your own, if it's any arbitrary object then I highly recommend using a library, but if no libraries then I guess you could try homerolling your own JSON serializer with reflection. More on reddit.com
🌐 r/javahelp
12
1
November 30, 2018
streaming a large json object to a file to avoid memory allocation error

Well, JSON.stringify and JSON.parse are synchronous methods that unfortunately you can't pipe to and from. Like u/nissen2 suggests, you'll find some modules out there that will provide you what you're looking for.

Ah, I think I got it. Is there a way to permanently increase the amount of memory that node has access to? Thx.

While this may solve your problem in the mean time, I wouldn't suggest simply allocating more memory (especially with a huge JSON object.)

More on reddit.com
🌐 r/node
16
0
October 13, 2014
🌐
Medium
medium.com › @benpourian › how-to-create-a-java-object-from-a-json-object-c47c08b030c5
How to create a Java Object from a JSON object | by Benjamin Pourian | Medium
October 25, 2018 - This will be a very brief guide to creating a Java object from a JSON object using the popular gson` google library. I will first demonstrate a simple JSON → POJO example then follow that with another example where I will be using annotations provided by gson to instantiate a new object by ...
🌐
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 previous lessons, you learned how to parse JSON files in Java and explore JSON's hierarchical structure. Now, we'll focus on constructing JSON objects and writing them to files. In this lesson, you'll discover how to create JSON objects using Java classes and serialize them using the Jackson library's ObjectMapper class.
🌐
Attacomsian
attacomsian.com › blog › jackson-create-json-object
How to create a JSON Object using Jackson
October 14, 2022 - A short tutorial to learn how to create a JSON object using Jackson API.
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonObjectBuilder.html
JsonObjectBuilder (Java(TM) EE 7 Specification APIs)
A builder for creating JsonObject models from scratch. This interface initializes an empty JSON object model and provides methods to add name/value pairs to the object model and to return the resulting object.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-generate-json-with-jsongenerator-in-java
How to Generate JSON with JsonGenerator in Java? - GeeksforGeeks
June 8, 2022 - The JsonObjectBuilder interface acts as a builder for creating JsonObject models from scratch. This interface initializes an empty JSON object model and provides methods to add name/value pairs to the object model and to return the resulting object.
Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-json-example
Java JSON Example | DigitalOcean
August 4, 2022 - The factory classes provide all the various ways to create these objects. javax.json.JsonObject: JsonObject represents an immutable JSON object value. Let’s look into the usage of Java JSON API with simple program, we have a JSON object stored in a file employee.txt as;
🌐
Camunda
forum.camunda.io › camunda 7 topics › discussion & questions
Creating JSON object in JAVA client to post into REST API - Discussion & Questions - Camunda Forum
April 22, 2016 - Hi, I am writing a java client that makes REST calls to Camunda. I understand the following code should allow me to create JSON string that is correctly formatted so that it can be posted into the API ( as per the variable format we see here: https://docs.camunda.org/manual/7.4/reference/rest/process-definition/post-start-process-instance/#request ). My example code is this package sandbox.camunda; import org.camunda.bpm.engine.variable.Variables; import org.camunda.bpm.engine.variable.valu...
🌐
TutorialsPoint
tutorialspoint.com › how-to-write-create-a-json-file-using-java
How to Write/create a JSON file using Java?
May 7, 2025 - Insert the required key-value pairs using the put() method of the JSONObject class. ... FileWriter file = new FileWriter("E:/output.json"); file.write(jsonObject.toJSONString()); file.close(); Below is an example of a Java program that creates a JSON object and writes it into a file named ...
🌐
Javatpoint
javatpoint.com › java-json-example
Java JSON Example
May 5, 2025 - Java JSON example for beginners and professionals with examples of JSON with java, install json.simple, java json encode, java json encode using map, java json array encode, java json array encode using List, java json decode. Learn Java JSON example with array, object, schema, encode, decode, ...
🌐
TutorialsPoint
tutorialspoint.com › json › json_java_example.htm
JSON with Java
Following is another example that shows a JSON object streaming using Java JSONObject − · import org.json.simple.JSONObject; class JsonEncodeDemo { public static void main(String[] args) { JSONObject obj = new JSONObject(); obj.put("name","foo"); obj.put("num",new Integer(100)); obj.put("balance",new Double(1000.21)); obj.put("is_vip",new Boolean(true)); StringWriter out = new StringWriter(); obj.writeJSONString(out); String jsonText = out.toString(); System.out.print(jsonText); } }
🌐
Crunchify
crunchify.com › json tutorials › how to write json object to file in java? simplifying json file handling in java
How to write JSON object to File in Java? Simplifying JSON File Handling in Java • Crunchify
August 17, 2023 - Before we dive into the code, make sure you have a Java development environment set up. You’ll also need the json-simple library, which provides a straightforward way to work with JSON data. You can include the library in your project through your preferred build tool or by manually adding the JAR file. Our goal is to create a JSON object representing some basic information.
🌐
TutorialsPoint
tutorialspoint.com › how-to-create-a-json-object-using-object-model-in-java
How to create a JSON Object using Object Model in Java?
July 7, 2020 - public static JsonObjectBuilder createObjectBuilder() import java.io.*; import javax.json.*; public class JsonObjectTest { public static void main(String[] args) throws Exception { JsonObjectBuilder builder = Json.createObjectBuilder(); builder.add("Name", "Adithya"); builder.add("Designation", "Python Developer"); builder.add("Company", "TutorialsPoint"); builder.add("Location", "Hyderabad"); JsonObject data = builder.build(); StringWriter sw = new StringWriter(); JsonWriter jw = Json.createWriter(sw); jw.writeObject(data); jw.close(); System.out.println(sw.toString()); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › working-with-json-data-in-java
Working with JSON Data in Java - GeeksforGeeks
December 23, 2025 - JSON decoding means converting JSON data into Java objects. ... import org.json.simple.JSONObject; import org.json.simple.JSONValue; public class JavaJsonDecoding { public static void main(String[] args) { String jsonString ="{\"Full Name\":\"Ritu Sharma\",\"Tuition Fees\":65400.0,\"Roll No.\":1704310046}"; Object obj = JSONValue.parse(jsonString); JSONObject jsonObject = (JSONObject) obj; String name = (String) jsonObject.get("Full Name"); double fees = (Double) jsonObject.get("Tuition Fees"); long rollNo = (Long) jsonObject.get("Roll No."); System.out.println(name + " " + fees + " " + rollNo); } }
🌐
JavaMadeSoEasy
javamadesoeasy.com › 2018 › 09 › create-json-using-javaxjsonjsonobjectbu.html
JavaMadeSoEasy.com (JMSE): Create JSON using javax.json.JsonObjectBuilder - Write int, String and Array in java
We can use Jackson api for for processing JSON in java. Jackson JSON examples · Convert JSON string to java Object - using Jackson · Convert java object to JSON string and pretty print Json in java - using Jackson · convert JSON string to java Map - using Jackson ·
🌐
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!

🌐
Tabnine
tabnine.com › home page › code › java › org.json.jsonobject
org.json.JSONObject.put java code examples | Tabnine
June 27, 2019 - public String creatingJsonString() { JSONArray pets = new JSONArray(); pets.put("cat"); pets.put("dog"); JSONObject person = new JSONObject(); person.put("name", "John Brown"); person.put("age", 35); person.put("pets", pets); return person.toString(2); } ... JSONObject jo = new JSONObject(); jo.put("firstName", "John"); jo.put("lastName", "Doe"); JSONArray ja = new JSONArray(); ja.put(jo); JSONObject mainObj = new JSONObject(); mainObj.put("employees", ja); ... /** * Create a JSON object that represents this data structure.
🌐
IBM
ibm.com › support › pages › creating-json-string-json-object-and-json-arrays-automation-scripts
Creating a JSON String from JSON Object and JSON Arrays in Automation Scripts
For clarification, The JSON body holds data from Maximo objects which will be sent to the external system to create associated records there. For single objects we use JSONObject alone which is an easy task. You can see the example code piece below. # creating a JSON String (directly executed via Run Automation Script button) from com.ibm.json.java import JSONObject from sys import * # method for creating a JSON formatted String def createJSONstring(): obj = JSONObject() obj.put('FIELD_1', 'VALUE_1') obj.put('FIELD_2', 0) obj.put('FIELD_3', 1.1) obj.put('FIELD_4', True) # add as many fields as needed ...
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonObject.html
JsonObject (Java(TM) EE 7 Specification APIs)
javax.json · All Superinterfaces: ... It also provides unmodifiable map view to the JSON object name/value mappings. A JsonObject instance can be created from an input source using JsonReader.readObject()....