I'm trying to integrate paypal payouts feature in my spring boot app and when I get response from the API I want to check weather it was successfull or not.
The documentation says the response is going to be in json, but then in the example it has type a String.
String response = s.hasNext() ? s.next() : "";
I want simnple to read the value of batch_status from this followong response. How should I do this? how can I access values from the response? thanks
{
"batch_header": {
"sender_batch_header": {
"sender_batch_id": "Payouts_2020_100007",
"email_subject": "You have a payout!",
"email_message": "You have received a payout! Thanks for using our service!"
},
"payout_batch_id": "2324242",
"batch_status": "PENDING"
}
}
Well I tried to convert it to json but it did not work.
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties
public class PayOutResponse {
private String batch_status;
private String payout_batch_id;
}
ObjectMapper mapper = new ObjectMapper();
PayOutResponse responseObject = mapper.readValue(response, PayOutResponse.class);
String status = responseObject.getBatch_status();
error:
Unrecognized field "batch_header" (class com.selector.model.PayOutResponse), not marked as ignorable
After updating @JsonIgnoreProperties(ignoreUnknown=true) now status is null
The simplest way for Jackson to access your variables is to provide getters like the following:
Alternatively, You need to use both @JsonProperty and @JsonCreator in conjunction like so:
Running the following code:
will give an output of:
I decided to actually use your sample json instead of a random sample json. Here is the code I used:
Output:
its not that the earlier code doesnt work. Its just that it wasnt applicable to your json due to nested objects.
edit: I was checking something up, and apparently as of 1.9 Jackson provides a DeserializationFeature to unwrap the top level root elements. See the new code that is much simpler. Note the @JsonRootElement that indicates the name of the pojo that should be deserialized (without it, Jackson will look for a field named after your pojo, not batch_header)