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
๐ŸŒ
JSON Formatter
jsonformatter.org โ€บ json-stringify-online
JSON Stringify Online using JSON.Stringify()
JSON Stringify Online helps convert string value to JSON String using JSON.Stringify(). It's very simple and easy way to create JSON String value and share Stringify data.
๐ŸŒ
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!

๐ŸŒ
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.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ json โ€บ introduction to json-java (org.json)
Introduction to JSON-Java | Baeldung
June 20, 2025 - JSON (JavaScript Object Notation) is a lightweight data-interchange format, and we most commonly use it for client-server communication. Furthermore, itโ€™s both easy to read/write and language-independent. A JSON value can be another JSON object, array, number, string, boolean (true/false), or null. 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.
๐ŸŒ
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.
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 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 ...
๐ŸŒ
Liquid Technologies
liquid-technologies.com โ€บ online-json-to-schema-converter
Free Online JSON to JSON Schema Converter
The Free Community Edition of Liquid Studio comes with an advanced JSON Editor, packed with many helpful features including JSON schema creation.
๐ŸŒ
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.
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ write json to a file with jackson
Write JSON to a file with Jackson - Mkyong.com
April 25, 2024 - package com.mkyong.json.jackson.tips; import com.fasterxml.jackson.databind.ObjectMapper; import com.mkyong.json.model.Person; import java.io.File; import java.io.IOException; public class WriteJsonToFileExample { public static void main(String[] args) { Person person = new Person("mkyong", 42); ObjectMapper om = new ObjectMapper(); try { // create a file object File file = new File("person.json"); // write JSON to a File om.writeValue(file, person); System.out.println("Filed saved to: " + file.getAbsolutePath()); } catch (IOException e) { throw new RuntimeException(e); } } } Output ยท
๐ŸŒ
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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-write-create-a-json-file-using-java
How to Write/create a JSON file using Java?
May 7, 2025 - Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc. ... { "book": [ { "id": "01", "language": "Java", "edition": "third", "author": "Herbert Schildt" }, { "id": "07", "language": "C++", "edition": "second", "author": "E.Balagurusamy" } ] } The following are the different Libraries for writing/creating a JSON file in Java:
๐ŸŒ
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. In the Listing 3, lines 1 through 3 lines create JsonReader; line 5 creates JsonObject for the results; line 7 loops over each result; and lines 8 through 11 get the name of the person who posted, get the public post, and prints them.
๐ŸŒ
Oracle
docs.oracle.com โ€บ javaee โ€บ 7 โ€บ api โ€บ javax โ€บ json โ€บ JsonObjectBuilder.html
JsonObjectBuilder (Java(TM) EE 7 Specification APIs)
javax.json ยท public interface JsonObjectBuilder ยท 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.
๐ŸŒ
Jsonschema2pojo
jsonschema2pojo.org
jsonschema2pojo
Generate Plain Old Java Objects from JSON or JSON-Schema.
๐ŸŒ
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...
๐ŸŒ
JSON
json.org
JSON
JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.
๐ŸŒ
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.