user1459497
user1459497

Reputation: 657

Map a dynamic json key to a class field using JPA

I am trying to map a json object to an Entity class. My Json is as follows:

    {
   
    "availabilities": {
    "2020-07-14T00:00:00Z": "Not Available",
    "2020-08-14T00:00:00Z": "Not Available",
    "2020-06-14T00:00:00Z": "Not Available",
    "2020-05-14T00:00:00Z": "Not Available",
    "2020-00-14T00:00:00Z": "Not Available"

      }
    }

I created a entity class using spring data :

@Entity
public class Availabilities {


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;


private String date;
private String availibility;



public String getDate() {
    return date;
}
public void setDate(String date) {
    this.date = date;
}
public String getAvailibility() {
    return availibility;
}
public void setAvailibility(String availibility) {
    this.availibility = availibility;
}

}

But i am getting the following error:

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "2020- 
07-14T00:00:00Z" (class org.abc.hotel.booking.model.Availabilities)

Please help.

Upvotes: 1

Views: 282

Answers (1)

Hiep Le
Hiep Le

Reputation: 373

your code is trying map by key-value, that mean your variable is the key. in your case. json look like this will work

// map to Availabilities
      {
        "date": "2020-07-14T00:00:00Z",
        "availibility": "Not Available",
      }

if you want to parse into a list => How to convert JSON string into List of Java object?

Upvotes: 1

Related Questions