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------");
        }
    }
}
Answer from Kishor Mandve on Stack Overflow
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();
  }
Discussions

How to convert JSON string into List of Java object? - Stack Overflow
Copy List items = ... = new ObjectMapper(); final File file = ResourceUtils.getFile("classpath:" + jsonFileName); CollectionType listType = mapper.getTypeFactory() .constructCollectionType(ArrayList.class, tClass); List ts = mapper.readValue(file, listType); ... 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
Getting JSON File from resources and reading to a List of Java Objects - Stack Overflow
i'm having a problem with GSON, i'm trying to get a json file and transform them into a list of objects, i have no idea how to solve that, i tried to follow the article below but im recieving this ... More on stackoverflow.com
🌐 stackoverflow.com
June 7, 2022
How to read json file into java with simple JSON library - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. I want to read this JSON file with java using json simple library. 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 ...
🌐
Crunchify
crunchify.com › how-to-read-json-object-from-file-in-java
How to Read JSON Object From File in Java?
February 16, 2023 - We cannot provide a description for this page right now
🌐
TutorialsPoint
tutorialspoint.com › article › how-can-we-convert-a-json-array-to-a-list-using-jackson-in-java
How can we convert a JSON array to a list using Jackson in Java?
June 5, 2025 - import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.List; import java.io.IOException; import java.util.ArrayList; public class JsonArrayToList{ public static void main(String[] args) { String jsonArray = "[{"name":"John","age":30},{"name":"Jane","age":25}]"; ObjectMapper objectMapper = new ObjectMapper(); try { List<Person> personList = objectMapper.readValue(jsonArray, new TypeReference<List<Person>>(){}); for (Person person : personList) { System.out.println("Name: " + person.getName() + ", Age: " + person.getAge()); } }
🌐
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:
Find elsewhere
🌐
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 ...
🌐
Stack Overflow
stackoverflow.com › questions › 72530365 › getting-json-file-from-resources-and-reading-to-a-list-of-java-objects
Getting JSON File from resources and reading to a List of Java Objects - Stack Overflow
June 7, 2022 - Gson gson = new Gson(); // create a reader Reader reader = Files.newBufferedReader(Paths.get("test.json")); // convert JSON array to list of items List<Item> items= new Gson().fromJson(reader, new TypeToken<List<Item>>() {}.getType()); // print ...
🌐
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 ...
🌐
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
🌐
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); ...
🌐
Attacomsian
attacomsian.com › blog › gson-read-json-file
How to read JSON from a file using Gson in Java
October 14, 2022 - Let us say we have the following ... ] You can now read a list of User objects from the above JSON file by using the same fromJson() method, as shown below:...
🌐
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. ... Copy{"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: CopyList<MyClass> myObjects = mapper.readValue(jsonInput, mapper.getTypeFactory().constructCollectionType(List.class, MyClass.class));
🌐
HowToDoInJava
howtodoinjava.com › home › gson › gson – parse json array to java array or list
Gson - Parse JSON Array to Java Array or List
April 4, 2023 - Learn to use Google GSON library to deserialize or convert JSON, containing JSON array as root or member, to Java Array or List of objects.
🌐
GitHub
gist.github.com › cblunt › 7865d8afc566287a9d4a
Parsing list of objects in JSON · GitHub
Parsing list of objects in JSON · Raw · ProductsActivity.java · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
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.
Top answer
1 of 10
14

This is a working example based (and tested) with gson-2.8.0. It accepts an arbitrary sequence of JSON objects on a given input stream. And, of course, it does not impose any restrictions on how you have formatted your input:

       InputStream is = /* whatever */
       Reader r = new InputStreamReader(is, "UTF-8");
       Gson gson = new GsonBuilder().create();
       JsonStreamParser p = new JsonStreamParser(r);

       while (p.hasNext()) {
          JsonElement e = p.next();
          if (e.isJsonObject()) {
              Map m = gson.fromJson(e, Map.class);
              /* do something useful with JSON object .. */
          }
          /* handle other JSON data structures */
       }
2 of 10
10

I know it has been almost one year for this post :) but i am actually reposing again as an answer because i had this problem same as you Yuan

I have this text.txt file - I know this is not a valid Json array - but if you look, you will see that each line of this file is a Json object in its case alone.

{"Sensor_ID":"874233","Date":"Apr 29,2016 08:49:58 Info Log1"}
{"Sensor_ID":"34234","Date":"Apr 29,2016 08:49:58 Info Log12"}
{"Sensor_ID":"56785","Date":"Apr 29,2016 08:49:58 Info Log13"}
{"Sensor_ID":"235657","Date":"Apr 29,2016 08:49:58 Info Log14"}
{"Sensor_ID":"568678","Date":"Apr 29,2016 08:49:58 Info Log15"}

Now I want to read each line of the above and parse the names "Sensor_ID" and "Date" into Json format. After long search, I have the following:

Try it and look on the console to see the result. I hope it helps.

package reading_file;

import java.io.*;
import java.util.ArrayList;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class file_read {
public static void main(String [] args) {
    ArrayList<JSONObject> json=new ArrayList<JSONObject>();
    JSONObject obj;
    // The name of the file to open.
    String fileName = "C:\\Users\\aawad\\workspace\\kura_juno\\data_logger\\log\\Apr_28_2016\\test.txt ";

    // This will reference one line at a time
    String line = null;

    try {
        // FileReader reads text files in the default encoding.
        FileReader fileReader = new FileReader(fileName);

        // Always wrap FileReader in BufferedReader.
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null) {
            obj = (JSONObject) new JSONParser().parse(line);
            json.add(obj);
            System.out.println((String)obj.get("Sensor_ID")+":"+
                               (String)obj.get("Date"));
        }
        // Always close files.
        bufferedReader.close();         
    }
    catch(FileNotFoundException ex) {
        System.out.println("Unable to open file '" + fileName + "'");                
    }
    catch(IOException ex) {
        System.out.println("Error reading file '" + fileName + "'");                  
        // Or we could just do this: 
        // ex.printStackTrace();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}