Read JSON file and convert to List in Java

In this post we will provide an example of how to read a JSON file and convert its contents to a list in Java. The JSON file will consist of an array of objects, the file will be read and each array object will be converted to a Java object.

Setup:

You will need to include the following dependancy in your maven project.

Read JSON file and convert to List example:

Assume we have the following JSON file called jsonFile.txt :

And we have the following Java objects defined (User and Address).

The example below, is using GSON to read the file named jsonFile.txt and converting it to a List of User objects by using a TypedToken. The TypedToken tell GSON how you want the JSON translated, in our case a list of user objects.

Running the above example produces the following:

If you have any questions or comments drop them below

Similar posts:

  1. Write a list to a file as JSON
  2. JSON to a map using jackson
  3. Convert InputStream to File
  4. CountDownLatch in Java

References:

  1. GSON documentation

 

Leave a Reply