How to read JSON File from a URL in Java

Learn different ways of Downloading and Reading JSON responses from a URL using Java.

Read JSON From a URL using Plain Java

One of the quickest and simplest ways of reading JSON responses from a URL is using the URL class and a buffered input stream reader.

Here, we open an InputStream to the server’s response and create InputStreamReader and then BufferedStreamReader to read the response line by line.

URL url = new URL(from);
StringBuilder builder = new StringBuilder();

try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) {
  String str;
  while ((str = bufferedReader.readLine()) != null) {
    builder.append(str);
  }
}
String jsonStr = builder.toString();Code language: Java (java)

Read JSON From a URL using Commons IO Library

The Commons IO library is a set of utility classes and methods to assist with developing IO functionality. We can use the Apache Commons IO library to read JSON from a URL without directly dealing with the streams.

Let’s add the Commons IO dependency with the latest version.

<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>{commons.io.version}</version>
</dependency>Code language: HTML, XML (xml)

Now, use the utility class IOUtils to read the JSON responses from the given URL.

URL url = new URL(from);
String jsonStr = IOUtils.toString(url, UTF_8);Code language: Java (java)

Read JSON From a URL using Jackson ObjectMapper

In the previous examples, we read the JSON responses from the server as a String. However, we can use Jackson’s Object Mapper to quickly read JSON responses from a server and convert them into custom or generic objects.

Firstly, let’s add the required maven dependency.

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>{jackson.databind.version}</version>
</dependency>Code language: HTML, XML (xml)

Reading JSON Responses into Generic Objects

We can use Jackson’s ObjectMapper to read JSON from URL inline and convert JSON data into generic objects.

JsonNode json = new ObjectMapper().readTree(new URL(from));Code language: Java (java)

The JsonNode is a generic object representing JSON nodes and providing easy-to-use access methods. In our example, we can use the JsonNode instance to access a specific node of the JSON response.

> json.get("info");
{"seed":"8237549306dc7107","results":1,"page":1,"version":"1.4"}Code language: Bash (bash)

Reading JSON Responses into Custom Objects

Previously, we read a JSON response into a JsonNode instance that we can generically use to hold any JSON data. However, we may want to read and convert a JSON response into a custom Java object. Thankfully, the Jackson ObjectMapper provides a way to do so.

Let’s create a POJO that can hold the JSON response data.

public class Response {
    List<Object> results = new ArrayList<>();
    Info info;

    // Getter, Setter, and Constructors
}Code language: Java (java)

Now, we can use ObjectMapper to download a JSON response from the given URL and parse it into a custom object.

Response response = new ObjectMapper().readValue(new URL(from), Response.class);Code language: Java (java)

Summary

This practical tutorial taught us how to read a JSON file from a URL in Java. We also learned that we could read the JSON file as a String, a generic object, or a custom object.

Refer to our GitHub Repository for the complete source code of the examples used in this tutorial.