HOWTO · Java
How to Read a JSON Array from a File in Java
Read a JSON array from a file in Java with the external JSON.simple library, then access its objects and nested values.
On this page
To read a JSON array from a file in Java, add a JSON parser such as JSON.simple to your project, parse the file, and traverse the resulting array. Java does not include a general-purpose JSON parser in its standard libraries. JSON.simple is an external dependency, so add it using the build tool or dependency manager for your project.
JSON.simple has had more than one release and API line. The example below uses the org.json.simple API family; before using it, choose a release and check that release’s documentation for its dependency coordinates, parser API, and value types. No particular release or build tool is assumed here. If your selected release uses different packages or method signatures, adapt the example to its documentation rather than copying an unrelated dependency declaration.
Handling JSON Arrays in Java
JSON (JavaScript Object Notation) represents data using objects, arrays, strings, numbers, booleans, and null. Java code needs a parser to turn JSON text into values it can work with. JSON.simple is one option for that parsing step.
An array is an ordered sequence enclosed in square brackets. An object is a collection of named values enclosed in braces. Arrays and objects can be nested, so a file may begin with an array whose elements are objects, each containing further arrays or objects.
JSON Objects and JSON Arrays in Java
An object stores values under string keys, with a colon between each key and value and commas between entries. An array stores values by position and separates them with commas. For example, the inline JSON value [1, "abc", {"name":"xyz"}] is an array containing a number, a string, and an object. The object has a name key whose value is a string.
A file used in the example has a top-level array with one object. That object contains a string, a number, two nested arrays, and a nested object:
[{"name":"MMN","roll":24,"marks":[91,95,97,97,94],"parents":{"name":"IMT","mobile":"1234567890"},"hobbies":["sports","reading","music"],"others":["A",1]}]
The shape matters when extracting values: code that reads name, roll, or parents assumes those keys exist and hold the expected JSON types. For input from outside your control, validate each value before treating it as a particular Java type.
Read a JSON Array from a File in Java with JSON.simple
Add JSON.simple through your project’s dependency manager. Use the artifact coordinates documented for the exact release you selected; this article does not prescribe coordinates because no particular release or build tool is established. The org.json.simple package family is external, so its imports and parser calls cannot run in a Java-only environment without the matching library on the classpath.
For a project using a compatible JSON.simple release, the parsing step is conceptually new JSONParser().parse(reader). Confirm the exact parser API and returned collection types in that release’s documentation. The example below focuses on the next step: traversing the Java collection structure after a parser has produced it. Its values match the sample file above, and it uses only Java standard-library collections so it can run on its own.
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class JSONArrayTraversal {
public static void main(String[] args) {
Map<String, Object> parents = new LinkedHashMap<>();
parents.put("name", "IMT");
parents.put("mobile", "1234567890");
Map<String, Object> record = new LinkedHashMap<>();
record.put("name", "MMN");
record.put("roll", 24);
record.put("marks", Arrays.asList(91, 95, 97, 97, 94));
record.put("parents", parents);
record.put("hobbies", Arrays.asList("sports", "reading", "music"));
record.put("others", Arrays.asList("A", 1));
List<Map<String, Object>> records = Arrays.asList(record);
for (Map<String, Object> item : records) {
System.out.println("Name: " + item.get("name"));
System.out.println("Roll: " + item.get("roll"));
System.out.println("Marks: " + item.get("marks"));
System.out.println("Hobbies: " + item.get("hobbies"));
Map<?, ?> parent = (Map<?, ?>) item.get("parents");
System.out.println("Parent's Name: " + parent.get("name")
+ ", Mobile: " + parent.get("mobile"));
System.out.println("Others: " + item.get("others"));
}
}
}
Name: MMN
Roll: 24
Marks: [91, 95, 97, 97, 94]
Hobbies: [sports, reading, music]
Parent's Name: IMT, Mobile: 1234567890
Others: [A, 1]
In an application, pass the value returned by the selected JSON.simple parser to traversal code adapted to that release’s collection types. Here, records stands in for the parsed top-level array. The casts and field lookups depend on the sample schema; validate each array element and field before relying on its type when input can vary. Keep the reader open while parsing and close it after parsing, including when parsing fails. Handle file access and parse failures according to the selected release’s documented behavior and the needs of your application. Supply a path that exists and is readable in your environment when connecting this traversal to file input.
Conclusion
A JSON array is an ordered sequence, while a JSON object associates named values with keys. To read one from a file in Java, use an external parser such as JSON.simple, add the dependency documented for the release in your project, then traverse the parsed structure according to the input schema. Check the selected release’s API and numeric value types before relying on casts or conversions.