I would suggest that you just make a simple ArrayList with your objects, and then serialize them into JSON with a serializer (Using the Jackson library in the example below). It would look something like this:

First, define your model in a class (Made without incapsulations for readability):

public class Country{
  public String name;
  public Integer population;
  public List<String> states;
}

Then you can go ahead and create it, and populate the list:

import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class JsonWriter {  

  public static void main(String[] args) {  

    Country countryObj = new Country();  
    countryObj.name = "India";
    countryObj.population = 1000000;

    List<String> listOfStates = new ArrayList<String>();  
    listOfStates.add("Madhya Pradesh");  
    listOfStates.add("Maharastra");  
    listOfStates.add("Rajasthan");  

    countryObj.states = listOfStates ;  
    ObjectMapper mapper = new ObjectMapper();

    try {  

        // Writing to a file   
        mapper.writeValue(new File("c:\\country.json"), countryObj );

    } catch (IOException e) {  
        e.printStackTrace();  
    }  

  }  
}
Answer from MichaelCleverly on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-read-and-write-json-files-in-java
How to Read and Write JSON Files in Java? - GeeksforGeeks
February 4, 2026 - It coverting Java obje­cts into JSON. Then an empty ObjectNode is created next. It helps structure of the JSON object. Then some Key-value­ pairs added to the JSON object. The­ put method adds them in. Put can add Strings, numbers, and othe­r types. At the last, ObjectMapper's write­Value method is used. It write­s the JSON object to a file calle­d "mydata.json".
🌐
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 - Simplifying JSON File Handling in Java: A Step-by-Step Guide with Logging. In this tutorial, I’ll show you how to write JSON data to a file using JSON.simple .
🌐
TutorialsPoint
tutorialspoint.com › how-to-write-create-a-json-file-using-java
How to Write/create a JSON file using Java?
May 7, 2025 - To create a JSON document using a Java program, the following are the steps: Instantiate the JSONObject class of the JSON-Simple library. ... Insert the required key-value pairs using the put() method of the JSONObject class. ... FileWriter file = new FileWriter("E:/output.json"); ...
🌐
Mkyong
mkyong.com › home › java › write json to a file with jackson
Write JSON to a file with Jackson - Mkyong.com
April 25, 2024 - We can use ObjectMapper.writeValue(file, object) to write the Person object to a file named person.json. ... package com.mkyong.json.jackson.tips; import com.fasterxml.jackson.databind.ObjectMapper; import com.mkyong.json.model.Person; import ...
🌐
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.
🌐
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 - JSON.simple is a lightweight JSON processing library that can be used to read and write JSON files and strings. The encoded/decoded JSON will be in full compliance with JSON specification (RFC4627). JSON.simple library is pretty old and has not been updated since march, 2012. Google GSON library is a good option for reading and writing JSON. In this Java JSON tutorial, we will first see a quick example of writing to a JSON file and then we will read JSON from the file.
🌐
Baeldung
baeldung.com › home › json › save data to a json file with gson
Save Data to a JSON File with GSON | Baeldung
January 8, 2024 - Learn how to convert Java Objects into JSON and write them into a File
Find elsewhere
🌐
Attacomsian
attacomsian.com › blog › java-read-write-json-files
How to read and write JSON Files in Java
October 3, 2022 - Learn how to read and write JSON files using JSON.simple, Jackson, Gson, and Mushi open-source libraries.
🌐
Stack Abuse
stackabuse.com › reading-and-writing-json-in-java
Reading and Writing JSON in Java
September 6, 2018 - This module is fully compliant with the JSON specification RFC4627 and provides core functionality such as encoding and decoding JSON objects and doesn't have any dependencies on external modules. Let's create a simple method that will take in a filename as a parameter and write some hardcoded JSON data:
🌐
Devstringx
devstringx.com › write-json-file-data
How to Write JSON File Data Using Java? - DS
July 1, 2025 - class JsonExample { public static void main(String args[]) { ObjectMapper mapper = new ObjectMapper(); String jsonPath = "C:\\Users\\ABC\\Documents\\API_testing\\jsonex\\sample.json"; try { JSONParser jsonParser = new JSONParser(); JSONObject obj = (JSONObject) jsonParser.parse(new FileReader(jsonPath)); obj.put("lastName", "Yadav"); ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter()); writer.writeValue(Paths.get(jsonPath).toFile(), obj); System.out.println("Name updated ........."); } catch (Exception e) { e.printStackTrace(); } } Good to Read:- How to Read Data from JSON files Using JAVA?
🌐
Websparrow
websparrow.org › home › how to write json object to file in java
How to write JSON object to File in Java - Websparrow
July 14, 2020 - JSONArray jsonArray = new JSONArray(); jsonArray.add("Java"); jsonObject.put("technology", jsonArray); Finally, use the FileWriter class to write the JSONObject into a file.
🌐
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.cliftonlabs.json_simple.JsonObject; import com.github.cliftonlabs.json_simple.Jsoner; import java.io.FileWriter; import java.io.IOException; public class JsonSimpleWriteExample1 { public static void main(String[] args) { // JSON String JsonObject jsonObject = new JsonObject(); jsonObject.put("name", "mkyong"); jsonObject.put("age", 42); // JSON Array JsonArray list = new JsonArray(); list.add("msg 1"); list.add("msg 2"); list.add("msg 3"); jsonObject.put("messages", list); try (FileWriter fileWriter = new FileWriter("person.json")) { Jsoner.serialize(jsonObject, fileWriter); } catch (IOException e) { throw new RuntimeException(e); } } }
🌐
Attacomsian
attacomsian.com › blog › jackson-write-json-file
How to write JSON to a file using Jackson
October 14, 2022 - The following example shows how you can write an instance of the Book class to a JSON file: try { // create book object Book book = new Book("Thinking in Java", "978-0131872486", 1998, new String[]{"Bruce Eckel"}); // create object mapper instance ObjectMapper mapper = new ObjectMapper(); // convert book object to JSON file mapper.writeValue(Paths.get("book.json").toFile(), book); } catch (Exception ex) { ex.printStackTrace(); }
🌐
CodeVsColor
codevscolor.com › how to write json data to a file in java - codevscolor
How to write json data to a file in Java - CodeVsColor
December 17, 2018 - In the above example, we have created one Student object s and convert it to a String first. toJson method is used to convert a Java object to a JSON string. Then, using a FileWriter, we wrote it to the file example.json.
🌐
TutorialsPoint
tutorialspoint.com › how-can-we-write-json-objects-to-a-file-in-java
How can we write JSON objects to a file in Java?\\n
The json.simple is a lightweight JSON processing library that can be used to write JSON files and it can be used to encode or decode JSON text and fully compliant with JSON specification(RFC4627). In order to read a JSON file, we need to download the json-simple.jar file and set the path to ...
🌐
Java2Blog
java2blog.com › home › json › json.simple example – read and write json
JSON.simple example – Read and write JSON - Java2Blog
March 22, 2017 - In this post,we will read and write JSON using JSON.simple. Download JSON.simple jar from here. Create a java project named “JSONSimpleExample” Create a folder jars and paste downloaded jar json-simple-1.1.1.jar.
🌐
Readthedocs
jse.readthedocs.io › en › latest › utils › gson › saveJsonFile.html
Save Data to a JSON File — Java Repositories 1.0 documentation
Also by default, Gson omits null fields during serialization. So if we consider this example: gson.toJson(new User(1, null, "Unknown"), new FileWriter(filePath)); ... User[] users = new User[] { new User(1, "Mike"), new User(2, "Tom") }; gson.toJson(users, new FileWriter(filePath));
🌐
GitHub
gist.github.com › whitfin › 8676531
Simple class to write a JSON object to a file, and read it back into JSON. Useful for data storing on Android. · GitHub
Simple class to write a JSON object to a file, and read it back into JSON. Useful for data storing on Android. - WriteObjectFile.java