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

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
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
can anyone tell me how to construct a custom json request in java
It's better that you not use a string and have variables in them. The best way to do it is have a class which resembles the structure of your json and then create an object of that class by setting the values you were setting for the json. Ex. class JsonRequest { String jsonrpc; String method; Map params; Integer id; } This resembles the structure of your request ( I have kept params as a map so you can add more params ) And after creating a class , you can instantiate an object with the required values. Ex. Map params = new HashMap<>(); params.put("account", 1920); JsonRequest jr = new JsonRequest("2.0", "getAccount", params, 123) Assuming that you have an all arguments constructor After setting the values you can use libraries like Gson or ObjectMapper to convert this object to json string; You can just google this step. More on reddit.com
🌐 r/javahelp
3
1
April 22, 2021
Is it common that it's so difficult to handle JSON payloads in Java?
No? It's rather easy to work with JSON in Java, but Java remains a statically typed language. If you want to manipulate them dynamically, just deserialize them to a JsonTree-like object and work your way through that. Or maybe even a HashMap. More on reddit.com
🌐 r/java
22
7
December 20, 2019
🌐
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.
🌐
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.
🌐
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 capturing the relevant serialized name.
🌐
W3Docs
w3docs.com › java
How to create JSON Object using String?
You can also use the org.json library to create a JSON object from scratch, by adding key-value pairs to the object using the put method: JSONObject jsonObject = new JSONObject(); jsonObject.put("key1", "value1"); jsonObject.put("key2", "value2");
🌐
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
🌐
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); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › working-with-json-data-in-java
Working with JSON Data in Java - GeeksforGeeks
December 23, 2025 - JSON encoding means converting Java data into JSON format. Example: Create and Print JSON Object.
🌐
Stleary
stleary.github.io › JSON-java › org › json › JSONObject.html
JSONObject
If the object is null, return the NULL object. If it is an array or collection, wrap it in a JSONArray. If it is a map, wrap it in a JSONObject. If it is a standard property (Double, String, et al) then it is already wrapped. Otherwise, if it comes from one of the java packages, turn it into ...
🌐
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!

🌐
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()....
🌐
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...
🌐
Javatpoint
javatpoint.com › java-json-example
Java JSON Example
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, ...
🌐
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.
🌐
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
If you are beginner don't worry learn how to Create new Maven project - In 2 minutes · 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 ·
🌐
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 ...
🌐
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;
🌐
Reddit
reddit.com › r/javahelp › can anyone tell me how to construct a custom json request in java
r/javahelp on Reddit: can anyone tell me how to construct a custom json request in java
April 22, 2021 -

below is a copy of the string i want to edit and send but when i try to use a varible in the string i get a error ( string litral ) i think its because i not formating the escape char correcly but im lost can any show me how to use escape chars with in a string

httpPostRequests testing = new httpPostRequests("{\"jsonrpc\":\"2.0\",\"method\":\"getaccount\",\"params\":{\""\account\":1920},\"id\":123}","http://localhost:4003");

Top answer
1 of 3
2
It's better that you not use a string and have variables in them. The best way to do it is have a class which resembles the structure of your json and then create an object of that class by setting the values you were setting for the json. Ex. class JsonRequest { String jsonrpc; String method; Map params; Integer id; } This resembles the structure of your request ( I have kept params as a map so you can add more params ) And after creating a class , you can instantiate an object with the required values. Ex. Map params = new HashMap<>(); params.put("account", 1920); JsonRequest jr = new JsonRequest("2.0", "getAccount", params, 123) Assuming that you have an all arguments constructor After setting the values you can use libraries like Gson or ObjectMapper to convert this object to json string; You can just google this step.
2 of 3
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Java Guides
javaguides.net › 2019 › 07 › java-create-json-example-json.html
Java Create JSON Example - JSON Processing API
July 16, 2019 - In this post, we will learn how to create a JSON object from Java object using JSON-P library. Check out complete JSON-P tutorial at Java JSON Processing Tutorial.