javaspringjackson-databind

How can I deserialize the toplyrics into a list using the jackson-databind?


{
  "artist": {
    "id": "3ade68b5gd237eda3",
    "desc": "Kiss",
    "url": "/kiss/",
    "pic_small": "/kiss/images/profile.jpg",
    "pic_medium": "/kiss/images/kiss.jpg",
    "rank": {
      "pos": "350",
      "period": 202404,
      "views": "1372",
      "uniques": "936",
      "points": "5.0"
    },
    "genre": [
      {
        "name": "Hard Rock",
        "url": "/browse/style/hard-rock.html"
      },
      {
        "name": "Heavy Metal",
        "url": "/browse/style/heavy-metal.html"
      },
      {
        "name": "Rock",
        "url": "/browse/style/rock.html"
      },
      {
        "name": "Classic Rock",
        "url": "/browse/style/classic-rock.html"
      }
    ],
    "related": [
      {
        "id": "3ade68b5gb2c7eda3",
        "name": "Van Halen",
        "url": "/van-halen/"
      },
      {
        "id": "3ade68b5g2cd6eda3",
        "name": "Deep Purple",
        "url": "/deep-purple/"
      },
      {
        "id": "3ade68b4gdda6eda3",
        "name": "Black Sabbath",
        "url": "/black-sabbath/"
      },
      {
        "id": "3ade68b5g0187eda3",
        "name": "Ramones",
        "url": "/the-ramones/"
      },
      {
        "id": "3ade68b3g0c86eda3",
        "name": "Led Zeppelin",
        "url": "/led-zeppelin/"
      },
      {
        "id": "3ade68b5gb1b8eda3",
        "name": "Ozzy Osbourne",
        "url": "/ozzy-osbourne/"
      },
      {
        "id": "3ade68b6g28c9eda3",
        "name": "Rolling Stones",
        "url": "/the-rolling-stones/"
      },
      {
        "id": "3ade68b5g1bf7eda3",
        "name": "Queen",
        "url": "/queen/"
      },
      {
        "id": "3ade68b5g2297eda3",
        "name": "Scorpions",
        "url": "/scorpions/"
      },
      {
        "id": "3ade68b5g8227eda3",
        "name": "Iron Maiden",
        "url": "/iron-maiden/"
      }
    ],
    "toplyrics": {
      "item": [
        
        {
          "id": "3ade68b8gace4afa3",
          "desc": "Danger Us",
          "url": "/kiss/danger-us.html",
          "turl": "/kiss/danger-us-traducao.html"
        },
        {
          "id": "3ade68b6gbfebeda3",
          "desc": "Dark Light",
          "url": "/kiss/dark-light.html",
          "turl": "/kiss/dark-light-traducao.html"
        },
        {
          "id": "3ade68b8gb764efa3",
          "desc": "Deadly Weapons (The Elder demo)",
          "url": "/kiss/deadly-weapons-the-elder-demo.html"
        },
        
      ]
    }
  }
}

So I'm trying to deserialize this JSON, but I coudn't do that with the "toplyrics".

I used jackson-databind and was able to deserilize the artist name, but then when I tried to do the same with "toplyrics" the program returned an error NullPointerException saying that the response is null. Here are my files:

package com.pedruuv.sound.models;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public record ApiResponse(@JsonAlias("artist") ArtistData artist,
                        @JsonAlias("toplyrics") SongData lyrics,
                        @JsonAlias("albums") AlbumData album){

}

package com.pedruuv.sound.models;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public record SongData(@JsonAlias("item") List<Music> songs) {

}

package com.pedruuv.sound.models;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public record Music(@JsonAlias("desc") String title) {

}


Solution

  • In json toplyrics is child of artist so it should be same hierarchy for your classes . SongData should be inside of ArtistData

    e.g

    public record ArtistData(@JsonAlias("toplyrics") SongData lyrics) {
    }