Let us take a look at how to read a JSON file and convert it into Java Object using Jackson library,
Java POJO: Album.javaimport 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();
}
}
}
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- Install SonarLint on Visual Studio Code - HowTos
- How to check uptime of Linux/Unix/macOS system/server using console command? - Linux
- Fix: Jupyter Notebook says Not Trusted - Python
- Increase Android Emulator Timeout time - Android
- AVD Pixel_2_API_15 is already running. [Android Studio] - Android-Studio
- How to Get File Size using Python in Bytes/KB/MB or GB - Python
- [Fix] Office 365 Sharepoint One Drive Error: Something went wrong. This is a temporary issue - SharePoint
- Run Scripting Language (JavaScript, Python, Groovy, Ruby) in Java - Java