Write a list to a file as JSON

In this post we will provide two examples on how to write a list to a file as JSON in Java. The first example will use GSON and the other will use Jackson. Both libraries are very popular for JSON processing in Java.

Write a list to a file as JSON

Both examples are very similar, the examples below converts a list of user objects to JSON and then writes it to a file.  We initially write the opening [ array bracket and then write the user object as JSON separated by a , and then finally closing with ] array bracket.

GSON Example

Jackson Example

Running the above examples will produce a file containing the following JSON

I hope this post has helped you write JSON to a file. Drop your questions and comments down below

Similar Posts

  1. Convert list to JSON array
  2. Json to Map object using Jackson
  3. Java object to JSON using Jackson
  4. How to write to a file in java

References

  1. GSON documentation
  2. Jackson Documentation

Leave a Reply