🌐
DigitalOcean
digitalocean.com › community › tutorials › json-simple-example
json-simple example | DigitalOcean
August 4, 2022 - Above json-simple example produces following output. Name = Pankaj Kumar Age = 32 City = New York City = Bangalore City = San Francisco · That’s all for a quick roundup of json-simple. However if you want to work with complex JSON data, you should use Jackson or Gson. You can also give JSR353 a try that got added into Java ...
🌐
TutorialsPoint
tutorialspoint.com › json_simple › json_simple_quick_guide.htm
JSON.simple - Quick Guide
Encode a JSON Array - to String − Simple encoding. Encode a JSON Array - Streaming − Output can be used for streaming. Encode a JSON Array - Using List − Encoding by using the List. Encode a JSON Array - Using List and Streaming − Encoding by using List and to stream. Following example illustrates the above concepts. import java.io.IOException; import java.io.StringWriter; import java.util.LinkedList; import java.util.List; import org.json.simple.JSONArray; import org.json.simple.JSONValue; class JsonDemo { public static void main(String[] args) throws IOException { JSONArray list = ne
🌐
Mkyong
mkyong.com › home › java › read and write json using json.simple
Read and write JSON using JSON.simple - Mkyong.com
May 13, 2024 - The following example uses JSON.simple JsonObject and JsonArray to create JSON and write it to a file named preson.json. ... package com.mkyong.json.jsonsimple; import com.github.cliftonlabs.json_simple.JsonArray; import com.github.cliftonl...
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › json › json_java_example.htm
JSON with Java
The following example makes use of JSONObject and JSONArray where JSONObject is a java.util.Map and JSONArray is a java.util.List, so you can access them with standard operations of Map or List. import org.json.simple.JSONObject; import org.json.simple.JSONArray; import org.json.simple.parser.ParseException; import org.json.simple.parser.JSONParser; class JsonDecodeDemo { public static void main(String[] args) { JSONParser parser = new JSONParser(); String s = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]"; try{ Object obj = parser.parse(s); JSONArray array = (JSONArray)obj; System.out.pr
🌐
How to do in Java
howtodoinjava.com › home › java libraries › json.simple – read and write json
JSON.simple - Read and Write JSON in Java
October 1, 2022 - package com.howtodoinjava.demo.jsonsimple; import java.io.FileWriter; import java.io.IOException; import org.json.simple.JSONArray; import org.json.simple.JSONObject; public class WriteJSONExample { @SuppressWarnings("unchecked") public static void main( String[] args ) { //First Employee JSONObject employeeDetails = new JSONObject(); employeeDetails.put("firstName", "Lokesh"); employeeDetails.put("lastName", "Gupta"); employeeDetails.put("website", "howtodoinjava.com"); JSONObject employeeObject = new JSONObject(); employeeObject.put("employee", employeeDetails); //Second Employee JSONObject
🌐
Tabnine
tabnine.com › home page › code › java › org.json.simple.jsonobject
org.json.simple.JSONObject java code examples | Tabnine
January 13, 2026 - URL url = new URL(update); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); json = (JSONObject) new JSONParser().parse(new InputStreamReader(connection.getInputStream())); } catch (IOException | ParseException ex) { if (!json.containsKey("travis_build_number")) { return null; int buildNumber = Integer.valueOf((String) json.get("travis_build_number")); String version = (String) json.get("app_version"); String downloadUrl = String.format(Updater.DOWNLOAD_URL, version); download = new URL(downloadUrl); } catch (MalformedURLException ex) {
🌐
GeeksforGeeks
geeksforgeeks.org › java › working-with-json-data-in-java
Working with JSON Data in Java - GeeksforGeeks
December 23, 2025 - ... 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....
🌐
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.
Find elsewhere
🌐
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, file, date etc.
🌐
Baeldung
baeldung.com › home › json › introduction to json-java (org.json)
Introduction to JSON-Java | Baeldung
June 20, 2025 - One of JSONObject’s constructors takes a POJO as its argument. 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:
🌐
GitHub
github.com › fangyidong › json-simple
GitHub - fangyidong/json-simple: A simple Java toolkit for JSON. You can use json-simple to encode or decode JSON text. · GitHub
August 4, 2020 - A simple Java toolkit for JSON. You can use json-simple to encode or decode JSON text. - fangyidong/json-simple
Starred by 758 users
Forked by 343 users
Languages   Java
🌐
Java Guides
javaguides.net › 2019 › 07 › jsonsimple-tutorial-read-and-write-json-in-java.html
JSON.simple Tutorial - Read and Write JSON in Java
June 13, 2025 - In this example, we create an array with 3 user objects using JSONObject and JSONArray classes and write to a file "users.json": package net.javaguides.jsonsimple; import java.io.FileWriter; import java.io.IOException; import org.json.simpl...
🌐
Cliftonlabs
cliftonlabs.github.io › json-simple
json-simple
After redesigning the library to be backwards compatible it was decided that support for java 2 to 6 would be dropped in favor of generics (thus a 2.* version) as it was the most avidly requested feature. The library was also written in the package org.json.simple so a maven artifact under the org.json.simple groupId made sense.
🌐
Google Code
code.google.com › archive › p › json-simple
Google Code Archive - Long-term storage for Google Code Project Hosting.
Archive · Skip to content · The Google Code Archive requires JavaScript to be enabled in your browser · Google · About Google · Privacy · Terms
🌐
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.
Top answer
1 of 2
2

There are a couple of issues in your code. One issue is that the structure you create in the java code will not match the structure that you show above. This I will try to describe a bit below. The second issue is that you get a stackoverflow exception (which you know but don't know why).

The stackoverflow exception is thrown cause the program runs out of the stack memory assigned by the computer. Why you ask? Well, cause you create a recursive or cyclic JSON object. This isn't good, but its not that big a deal cause its kinda easy to fix. So why does the program throw this exception? Well, look at the following snippet:

JSONArray parameterlist = new JSONArray();
parameterlist.add(map);
parameterlist.add(jsonObject);

jsonObject.put("parameters", parameterlist);
jsonObject.put("function", "create_contact_group");

You create a JSONArray and then add the JSONObject created before to the array. After that you add the same array to the object that is already in the array. I expect that you see the issue with that!

So, that should not be done. And how to fix this? Well, I kinda think its better that I describe how you should write the code to get the structure you are actually asking for, so I'll try do that...

What to do...?

A JSON (JavaScript Object Notation) -object is always declared with this type of brackets: {} a JSON array with [], so, the JSON you are trying to generate should be in the following data types:

{  // <= Root object (a JSON-object).
    "function": "create_contact_group", // <= Key in the root-object (where the key is a string and the value a string)
    "parameters": [ // <= Key in the root-object (where the key is a string and the value is an array.
        {  // <= Object inside the array.
            "user_id": "teer", // Key (string/string)
            "comp_id": "97",   // Key (string/string)
            "contact_group_name": "Test01", // Key (string/string)
            "invite_user_list": [  // Key (string/array)
                {   // Object inside the invite_user_list array
                    "invite_user_id": "steve"  // Key (string/string)
                }
            ]
        }
    ]
}

So when creating the JSON-object in java, you will want to create a root object then add all the diff params inside it. Adding a value to a JSONObject is done with the JSONObject.put(string, Object) method, where the string is a key and the object a value.

So to start, I would recommend creating the parameters list. In your case, you use a HashMap for the objects, which is not really wrong, but not really necessary either, I would just stick to a JSONObject, which is not all that different than a HashMap<string, Object>. So instead of map.put(...), you could do something like:

JSONObject param = new JSONObject();
param.put("user_id", "teer");
param.put("comp_id", "97");
param.put("contact_group_name", "Test01");

Now, one of the objects values should be an array (invite_user_id) and the easiest way to add an array to the object is to create a JSONArray and then just add it.

JSONArray inviteList = new JSONArray();
// Then you need to add an object to the array for each `user` that has invited.
// For-loop here maybe?
JSONObject invitee = new JSONObject();
invitee.put("invite_user_id", user);
inviteList,add(invitee); // This will make it into an array with objects, I.E., [ { "invite_user_id": "Steve" } ]

After creating the invite list, add it to the param object like:

param.put("invite_user_list", inviteList);
// Now, param should be in its own list too, so you should probably create a JSONArray for the params.
// Ill leave that to you, and we pretend we have a list of the param objects named "params".

And then at the end, you create the root object and set its values:

JSONObject root = new JSONObject();
root.put("parameters", params);
root.put("function", "create_contact_group");

And that should be it.

This should create a JSON-string with the structure that you made above. But I would recommend testing (and writing unit tests!) for this (especially as I have written this code in the browser!).

But why?!

I guess I should try to describe why your code was not working as the one I described above.

You start by creating a root object, so far so good (can create it at start or at the time you need it, doesn't really matter), after that you create a HashMap and add the properties to it. So far this is also legit (you could later create a JSONObject from the map).

In the next part, you create an ArrayList (im not really sure why) and add a name to it, and then another HashMap which you add the single name to (key invite_user_list) inside a for-loop.
This is either not necessary (cause its just one name) or wrong (if there is supposed to be more names in a real life execution of the code), in case of unnecessary, the for-loop shouldn't be there and in case of "not like real life" it should not be added to a Map!
Instead the invieList should have been an array, and each entry added should have been a object which had the "invite_user_id" key set to the name.

After that, you add the inviteList HashMap to a newly created JSONArray, I guess this could be kinda okay, if you only want one object ever in the array, else I would recommend creating it before the loop and add each entry into it!
The inviteArray is then put inside the root object with the key invite_user_list, after that you create another JSONArray and add both the map (your parameters created at the start) and the JSONObject (root) created first of all.
But the thing you do after that, is why you are getting a stackoverflow exception, you add the parameterlist (which contains the jsonObject (root)) to the jsonObject, which makes the jsonObject exist inside an array that is inside itself!
This creates a cyclic JSON structure which will never end if the whole thing was to be unrolled, hence the computer throws the exception.

The structure of the resulting object would also be wrong, cause it would look something like this:

{ // Root (jsonObject)
    "invite_user_list": [
        { "invite_user_id": "steve" }
    ]
    "parameters": [
        { // The "map" hashmap
            "user_id", "teer",
            "comp_id": "97",
            "contact_group_name": "Test01"
        },
        {   // The jsonObject object (which is also the root!)
            "invite_user_list": [
                { "invite_user_id": "steve" }
            ],
            "parameters": [
                { // The "map" hashmap
                    "user_id", "teer",
                    "comp_id": "97",
                    "contact_group_name": "Test01"
                },
                {
                    // The jsonObject object again (which is also the root and the parent!)

                    // ... and so on til eternity!
                }
            ],
            "function": "create_contact_group" 
        }
    ],
    "function": "create_contact_group" 
}

Extra...

I would like to add here at the end (where I hope you end up after reading the whole wall of text that I wrote above, cause you might have learnt something!) that there is a easier way of doing it.

Now, I haven't used this lib myself, but from what I understand, it should be able to serialize a whole object, the lib can be found at Googles github repos which can be used as a json serializer and convert a class-instance to a json string, then you could just create a class for the whole object and fill it up and serialize it at the end of the function, without using either JSONArray nor JSONObject.

2 of 2
1

The issue is due to recursion process that occurs when you are trying to add the JsonObject to JsonArray and viceVersa.

The thing you are doing is,

 JSONArray parameterlist = new JSONArray();
 parameterlist.add(map);
 parameterlist.add(jsonObject);

And then

jsonObject.put("parameters", parameterlist);

The problem is when you print the object using jsonObject.toJSONString(), Then at first it will fetch the parameterlist then as jsonObject is part of the keyvalue pair on the parameterlist JsonArray it will refetch the jsonObject which then again fetch the parameterlist and this process continues on and hence causing the StackOverflow Issue.

The Quick Solution is to create new JsonObject while assigning the parameterList,

JSONArray parameterlist = new JSONArray();
parameterlist.add(map);
parameterlist.add(jsonObject);

JSONObject newJson = new JSONObject();
newJson.put("parameters", parameterlist);

System.out.println(newJson.toJSONString());
🌐
Oracle
docs.oracle.com › javaee › 7 › tutorial › jsonp001.htm
19.1 Introduction to JSON - Java Platform, Enterprise Edition: The Java EE Tutorial (Release 7)
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": ...
🌐
Centron
centron.de › startseite › json-simple example
json-simple Example
March 21, 2012 - Above json-simple example produces following output. ... That’s all for a quick roundup. However if you want to work with complex JSON data, you should use Jackson or Gson. You can also give JSR353 a try that got added into Java 7.