TransWikia.com

Map a dynamic json key to a class field using JPA

Stack Overflow Asked on November 16, 2021

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.

One Answer

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?

Answered by Hiep Le on November 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP