Read Json File and Convert to Java Object using Jackson


Post Banner

Let us take a look at how to read a JSON file and convert it into Java Object using Jackson library,

Java POJO: Album.java
import java.util.Arrays;

public class Album {

        private String artistName;
        private String albumName;
        private int releaseYear;
        private String[] songsList;

        public Album() {}

    public Album(String artistName, String albumName, int releaseYear, String[] songsList) {
        this.artistName = artistName;
        this.albumName = albumName;
        this.releaseYear = releaseYear;
        this.songsList = songsList;
    }

    public String getArtistName() {
        return artistName;
    }

    public void setArtistName(String artistName) {
        this.artistName = artistName;
    }

    public String getAlbumName() {
        return albumName;
    }

    public void setAlbumName(String albumName) {
        this.albumName = albumName;
    }

    public int getReleaseYear() {
        return releaseYear;
    }

    public void setReleaseYear(int releaseYear) {
        this.releaseYear = releaseYear;
    }

    public String[] getSongsList() {
        return songsList;
    }

    public void setSongsList(String[] songsList) {
        this.songsList = songsList;
    }

    @Override
    public String toString() {
        return "Album{" +
                "artistName='" + artistName + '\'' +
                ", albumName='" + albumName + '\'' +
                ", releaseYear=" + releaseYear +
                ", songsList=" + Arrays.toString(songsList) +
                '}';
    }
}

JSON file: album.json
{
   "artistName":"Michael Jackson",
   "albumName":"Thriller",
   "releaseYear":1982,
   "songsList":[
      "Wanna Be Startin Somethin",
      "Baby Be Mine",
      "The Girl Is Mine",
      "Thriller",
      "Beat it",
      "Billie Jean",
      "Human Nature",
      "P.Y.T. (Pretty Young Thing)",
      "The Lady in My Life"
   ]
}

JsonFileToJavaObject.java
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;

public class JsonFileToJavaObject {

    public static void main(String[] args) {

        try {
            String jsonFileName = "album.json";
            File jsonFile = new File(jsonFileName);
            ObjectMapper objectMapper = new ObjectMapper();
            
            //JSON File to Java Object
            Album album = objectMapper.readValue(jsonFile, Album.class);
            
            System.out.println(album.getAlbumName());
            System.out.println(album.getArtistName());
            System.out.println(album.getReleaseYear());
            
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Have Questions? Post them here!
Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.