The whole file is an array and there are objects and other arrays (e.g. cars) in the whole array of the file.

As you say, the outermost layer of your JSON blob is an array. Therefore, your parser will return a JSONArray. You can then get JSONObjects from the array ...

  JSONArray a = (JSONArray) parser.parse(new FileReader("c:\\exer4-courses.json"));

  for (Object o : a)
  {
    JSONObject person = (JSONObject) o;

    String name = (String) person.get("name");
    System.out.println(name);

    String city = (String) person.get("city");
    System.out.println(city);

    String job = (String) person.get("job");
    System.out.println(job);

    JSONArray cars = (JSONArray) person.get("cars");

    for (Object c : cars)
    {
      System.out.println(c+"");
    }
  }

For reference, see "Example 1" on the json-simple decoding example page.

Answer from Greg Kopff on Stack Overflow
Discussions

Reading a JSON File from resources and creating a List of Objects in Java - Stack Overflow
Basically, I have the JSON file below and I need to read him and add into a List of Objects in Java, Which library should I use in this case? My biggest difficulty is read the Json starting with the More on stackoverflow.com
🌐 stackoverflow.com
How to convert JSON string into List of Java object? - Stack Overflow
Now I want to convert it into Java object and store it in List of java object. e.g. In Student object. I am using below code to convert it into List of Java object : - ObjectMapper mapper = new ObjectMapper(); StudentList studentList = mapper.readValue(jsonString, StudentList.class); More on stackoverflow.com
🌐 stackoverflow.com
How to read JSON file of objects into a list in Java with Jackson - Stack Overflow
If I have an existing JSON file like above, how can I append more objects to the array in the file? 2017-05-19T06:45:22.837Z+00:00 ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... Should beginner writers publish short story science fiction on Amazon short reads ... More on stackoverflow.com
🌐 stackoverflow.com
How to read JSON to a list of generic objects in java? - Stack Overflow
I have to get data from a web service, I'm using Jackson but I have the same problem using Gson, I have no problem with single objects but when I receive several objects list it is not that easy fo... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Attacomsian
attacomsian.com › blog › jackson-read-json-file
How to Read JSON from a file using Jackson
October 14, 2022 - Suppose we have the following JSON ... } ] You can now read a list of Book objects from the above JSON file using the same readValue() method as shown below: try { // create object mapper instance ObjectMapper mapper = new ...
Top answer
1 of 3
1

There are lots of API's and libraries are present but I prefer to use org.json API suggested by json.org

you can also go for GSON library which is one of the best library for serialize and deserialize Java objects to (and from) JSON.

here's the quick demo of reading above JSON with org.json API.

import org.json.JSONObject;
import org.json.JSONArray;

public class HelloWorld {
    public static void main(String[] args) {
        String jsonString = "[ { \"name\": \"Andrew\", \"age\": 21, \"parents\": [ { \"name\": \"Joseph\", \"age\": 18 }, { \"name\": \"Joseph\", \"age\": 18 } ] }, { \"name\": \"Maria\", \"age\": 35, \"parents\": [ { \"name\": \"Kassandra\", \"age\": 16 }, { \"name\": \"Abigail\", \"age\": 22 } ] } ]";
        JSONArray json = new JSONArray(jsonString);
        for(int i=0; i<json.length(); i++){
          JSONObject j = json.getJSONObject(i);
          System.out.println(j + "\n------");
        }
    }
}
2 of 3
0

Use jackson library. Here is a snippet.

public static void main(final String[] args) throws JsonProcessingException {
    final List<Child> children = new ObjectMapper().readValue(
        readFromFile("data.json"), new TypeReference<List<Child>>() {
        });
    System.out.println(children);
  }

  public static String readFromFile(final String resourcePath) {
    final ClassPathResource resource = new ClassPathResource(resourcePath);

    try {
      final InputStream inputStream = resource.getInputStream();
      return readFromInputStream(inputStream);
    } catch (final IOException var4) {
      return "";
    }
  }

  private static String readFromInputStream(final InputStream inputStream) throws IOException {
    final StringBuilder resultStringBuilder = new StringBuilder();
    final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
    Throwable var3 = null;

    try {
      String line;
      try {
        while ((line = br.readLine()) != null) {
          resultStringBuilder.append(line).append("\n");
        }
      } catch (final Throwable var12) {
        var3 = var12;
        throw var12;
      }
    } finally {
      if (br != null) {
        if (var3 != null) {
          try {
            br.close();
          } catch (final Throwable var11) {
            var3.addSuppressed(var11);
          }
        } else {
          br.close();
        }
      }

    }

    return resultStringBuilder.toString();
  }
🌐
How to do in Java
howtodoinjava.com › home › convert json array to list: gson, jackson and org.json
Convert JSON Array to List: Gson, Jackson and Org.json
September 25, 2023 - List<Person> readPersonListFromJsonArray(String json) { JSONArray jsonArray = new JSONArray(json); List<Person> personList = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonPerson = jsonArray.getJSONObject(i); ...
🌐
Stack Abuse
stackabuse.com › converting-json-array-to-a-java-array-or-list
Convert JSON Array to a Java Array or List with Jackson
September 8, 2020 - Thankfully, Jackson makes this task as easy as the last one, we just provide the File to the readValue() method: final ObjectMapper objectMapper = new ObjectMapper(); List<Language> langList = objectMapper.readValue( new File("langs.json"), new TypeReference<List<Language>>(){}); langList.forEach(x ...
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 30633979 › how-to-read-json-to-a-list-of-generic-objects-in-java
How to read JSON to a list of generic objects in java? - Stack Overflow
I have to get data from a web service, I'm using Jackson but I have the same problem using Gson, I have no problem with single objects but when I receive several objects list it is not that easy for me. ... {"country": [ {"code":"AD","nombre":"Andorra","name":"Andorra"}, {"code":"AE","nombre":"Emiratos Árabes Unidos","name":"United Arab Emirates"} ] } This is a list of my own class CountryWSType, I have several classes like this and need a way that can get the list of any type of them. I've tried parse it like a list: List<MyClass> myObjects = mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, MyClass.class));
🌐
Mkyong
mkyong.com › home › java › how to parse json array with jackson
How to parse JSON Array with Jackson - Mkyong.com
April 23, 2024 - convert JSON array to List List<Person> person2 = mapper.readValue(jsonArray, new TypeReference<>() { }); person2.forEach(System.out::println); } } output · Person{name='mkyong', age=42} Person{name='ah pig', age=20} Person{name='mkyong', age=42} Person{name='ah pig', age=20} JsonArrayToObjectExample2.java · package com.mkyong.json.jackson; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.mkyong.json.model.Person; import java.util.Arrays; import java.util.List; public class JsonArrayToObjectExample2 { public static void
🌐
Attacomsian
attacomsian.com › blog › jackson-convert-json-array-to-from-java-list
Convert JSON array to a list using Jackson in Java
November 6, 2022 - To convert the JSON array into an equivalent Java array, you should do the following: User[] users = new ObjectMapper().readValue(json, User[].class); If your JSON array is stored in a JSON file, you can still read and parse its content to a list of Java Objects, as shown below:
Top answer
1 of 6
321

Definitely the easiest way to do that is using Gson's default parsing function fromJson().

There is an implementation of this function suitable for when you need to deserialize into any ParameterizedType (e.g., any List), which is fromJson(JsonElement json, Type typeOfT).

In your case, you just need to get the Type of a List<String> and then parse the JSON array into that Type, like this:

import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;

JsonElement yourJson = mapping.get("servers");
Type listType = new TypeToken<List<String>>() {}.getType();

List<String> yourList = new Gson().fromJson(yourJson, listType);

In your case yourJson is a JsonElement, but it could also be a String, any Reader or a JsonReader.

You may want to take a look at Gson API documentation.

2 of 6
19

Below code is using com.google.gson.JsonArray. I have printed the number of element in list as well as the elements in List

import java.util.ArrayList;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;


public class Test {

    static String str = "{ "+ 
            "\"client\":\"127.0.0.1\"," + 
            "\"servers\":[" + 
            "    \"8.8.8.8\"," + 
            "    \"8.8.4.4\"," + 
            "    \"156.154.70.1\"," + 
            "    \"156.154.71.1\" " + 
            "    ]" + 
            "}";

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {

            JsonParser jsonParser = new JsonParser();
            JsonObject jo = (JsonObject)jsonParser.parse(str);
            JsonArray jsonArr = jo.getAsJsonArray("servers");
            //jsonArr.
            Gson googleJson = new Gson();
            ArrayList jsonObjList = googleJson.fromJson(jsonArr, ArrayList.class);
            System.out.println("List size is : "+jsonObjList.size());
                    System.out.println("List Elements are  : "+jsonObjList.toString());


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

OUTPUT

List size is : 4

List Elements are  : [8.8.8.8, 8.8.4.4, 156.154.70.1, 156.154.71.1]
🌐
Crunchify
crunchify.com › json tutorials › how to read json object from file in java?
How to Read JSON Object From File in Java? • Crunchify
February 16, 2023 - hi i have a json file. i want to read the json file and i want to identify syntax error only from this file if it is there and want to print this error along with the row number.can u write the peace of standalone java code for this scenario. ... Hi Siba. Could you please share your question with all details on Crunchify forum? ... I tried to parse a json file of mine but it changed the object order.
🌐
Stack Overflow
stackoverflow.com › questions › 67858246 › how-can-i-convert-json-file-into-list-of-objects
java - How can I convert JSON file into List of objects? - Stack Overflow
In line 6 you call the readValue method on the ObjectMapper instance and pass the String created in line 3, which contains the file name. Does this method really want a String there with the file name? If this is the ObjectMapper from the Jackson library, you need to parse the JSON code as the first parameter to the readValue call, not the file name.
🌐
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 - First of all, we will create JSONParser instance to parse JSON file. Use FileReader to read JSON file and pass it to parser. Start reading the JSON objects one by one, based on their type i.e. JSONArray and JSONObject. package com.howtodoin...
🌐
Devstringx
devstringx.com › read-data-from-json
How to Read Data from JSON file Using JAVA? - Devstringx
July 1, 2025 - Now, use JAVA’s input or output classes, namely fileReader or BufferedReader, to read the content. Now you need to parse the JSON data into suitable JAVA object representations. Jsonfactory and ObjectMapperwhere are a few examples of multiple classes and methods for parsing JSON.
🌐
Delft Stack
delftstack.com › home › howto › java › read json file java
How to Read JSON File in Java | Delft Stack
February 2, 2024 - We need to import two classes from the java.simple library, the org.json.simple.JSONArray and org.json.simple.JSONObject class. The JSONArray helps us read elements in the form of an array, and the JSONObject helps us read the objects present ...
🌐
Jenkov
jenkov.com › tutorials › java-json › jackson-objectmapper.html
Jackson ObjectMapper
February 1, 2024 - Reading arrays of objects also works with other JSON sources than a string. For instance, a file, URL, InputStream, Reader etc. The Jackson ObjectMapper can also read a Java List of objects from a JSON array string.
🌐
Baeldung
baeldung.com › home › json › jackson › convert json array to java list
Convert JSON Array to Java List | Baeldung
August 13, 2025 - Then, we use the readValue() method of the ObjectMapper object to convert the JSON array String to a List. Similar to the assertion discussed previously, finally, we compare a specific field from the String JSON array to the jacksonList ...
Top answer
1 of 2
4

You should definitly have a look at the Jackson Streaming API (https://www.baeldung.com/jackson-streaming-api). I used it myself for GB large JSON files. The great thing is you can divide your JSON into several smaller JSON objects and then parse them with mapper.readTree(parser). That way you can combine the convenience of normal Jackson with the speed and scalability of the Streaming API.

Related to your problem:

I understood that your have a really large array (which is the reason for the file size) and some much more readable objects:

e.g.:

[ // 40GB
{}, // Only 400 MB
{},
]

What you can do now is to parse the file with Jackson's Streaming API and go through the array. But each individual object can be parsed as "regular" Jackson object and then processed easily.

You may have a look at this Use Jackson To Stream Parse an Array of Json Objects which actually matches your problem pretty well.

2 of 2
2

is there a way to read this file using BufferedReader and then to push object by object ?

Of course, not. Even you can open this file how you can store 40GB as java objects in memory? I think you don't have such amount of memory in you computers (but technically using ObjectMapper you should have about 2 times more operation memory - 40GB for store json + 40GB for store results as java objects = 80 GB).

I think you should use any way from this questions, but store information in databases or files instead of memory. For example, if you have millions rows in json, you should parse and save every rows to database without keeping it all in memory. And then you can get this data from database step by step (for example, not more then 1GB for every time).