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 Overfloworg.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"}
In contrast to what the accepted answer proposes, the documentation says that for JSONArray() you must use put(value) no add(value).
https://developer.android.com/reference/org/json/JSONArray.html#put(java.lang.Object)
(Android API 19-27. Kotlin 1.2.50)
Creating JSON object in JAVA client to post into REST API
Generate example JSON from Java classes
Convert Java object to JSON without external library
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.comVideos
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!