Java 8 – Convert Map to JSON Array

In this post we will demonstrate how to convert a map to a JSON array in Java 8 using GSON.

Setup:

You will need to include the following dependancy into your project.

Convert Map to JSON array:

The following example shows how to convert a map to a JSON array. The example uses the stream() method along with map and reduce. It converts each map entry into a JsonObject and then reduces it into a single JsonArray object which contains all the JsonObjects.

Output

In running the above code you get the following output

Conclusion:

I hope you found this post of use and it helps you solve your development issues

Similar posts:

  1. How to parse JSON array into a List
  2. How to convert Java object into JSON
  3. Java 8 reduce operation example
  4. Java 8 Stream map example

References:

  1. Java Streams API Documentation
  2. GSON Documentation

Leave a Reply