Convert JSON Array to a Java Array or List with Jackson

Introduction

In this article, we'll convert a JSON array into a Java Array and Java List using Jackson.

Since we're using Jackson, you'll have to add it to your project. If you're using Maven, it's as easy as adding the dependency:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.2</version>
</dependency>

Or, if you're using Gradle:

compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.11.2'

Since we're mapping from JSON to our own objects, let's go ahead and define a POJO:

public class Language {

    private String name;
    private String description;

    // Getters, setters and toString() method

Reading JSON from a String

Let's start out with reading JSON from a String. The String contains an array of programming languages, with brief descriptions:

String json = "[{\"name\": \"Java\", \"description\": \"Java is a class-based, object-oriented programming language.\"},{\"name\": \"Python\", \"description\": \"Python is an interpreted, high-level and general-purpose programming language.\"}, {\"name\": \"JS\", \"description\": \"JS is a programming language that conforms to the ECMAScript specification.\"}]";

Using Jackson's ObjectMapper class, it's easy to read values and map them to an object, or an array of objects. We just use the readValue() method, passing the JSON contents and the class we'd like to map to. Since we're mapping to an array of Language, we'll also specify this in the readValue() method:

// It's advised to use ObjectMapper as a singleton and reuse the instance
final ObjectMapper objectMapper = new ObjectMapper();
Language[] langs = objectMapper.readValue(json, Language[].class);

Alternatively, you can extract the values directly into a list, using Jackson's TypeReference:

List<Language> langList = objectMapper.readValue(json, new TypeReference<List<Language>>(){});

Without using TypeReference<>, which is advised, you can convert the array into a list with any other approach at your disposal, such as:

List<Language> langList = new ArrayList(Arrays.asList(langs));

And then print out the values:

langList.forEach(x -> System.out.println(x.toString()));

This results in:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

Language{name='Java', description='Java is a class-based, object-oriented programming language.'}
Language{name='Python', description='Python is an interpreted, high-level and general-purpose programming language.'}
Language{name='JS', description='JS is a programming language that conforms to the ECMAScript specification.'}

Reading JSON from a File

We don't always deal with JSON in String format. Oftentimes, the contents come from a File. Thankfully, Jackson makes this task as easy as the last one, we just provide the File to the readValue() method:

final ObjectMapper objectMapper = new ObjectMapper();
List<Language> langList = objectMapper.readValue(
        new File("langs.json"), 
        new TypeReference<List<Language>>(){});

langList.forEach(x -> System.out.println(x.toString()));

The langs.json file contains:

[
  {
    "name": "Java",
    "description": "Java is a class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible."
  },
  {
    "name": "Python",
    "description": "Python is an interpreted, high-level and general-purpose programming language. Created by Guido van Rossum and first released in 1991."
  },
  {
    "name": "JS",
    "description": "JS is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm."
  }
]

Running this code results in:

Language{name='Java', description='Java is a class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible.'}
Language{name='Python', description='Python is an interpreted, high-level and general-purpose programming language. Created by Guido van Rossum and first released in 1991.'}
Language{name='JS', description='JS is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm.'}

Conclusion

In this article, we've used Jackson to parse and map the values from a JSON String and file into a Java array and list.

This is done via the readValue() method, by specifying the JSON contents (String or file) and by specifying the POJO we'd like to map to.

Last Updated: September 8th, 2020
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

David LandupAuthor

Entrepreneur, Software and Machine Learning Engineer, with a deep fascination towards the application of Computation and Deep Learning in Life Sciences (Bioinformatics, Drug Discovery, Genomics), Neuroscience (Computational Neuroscience), robotics and BCIs.

Great passion for accessible education and promotion of reason, science, humanism, and progress.

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms