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
2
Answers
It seems like you are dealing with a JSON response, and you want to extract the value of the batch_status field from the JSON string. If you’ve tried converting it to JSON and it didn’t work, it might be due to incorrect parsing or handling.
Assuming you have the JSON response in the response variable as a string, you can use a JSON library in JavaScript to parse it and then access the desired value. Here’s an example using JavaScript:
// Assuming response is your JSON string
var response = ‘{"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" } }’;
// Parse the JSON string
var jsonResponse = JSON.parse(response);
// Access the batch_status value
var batchStatus = jsonResponse.batch_header.batch_status;
// Log the batch_status value
console.log(batchStatus);
This code assumes that the structure of your JSON response is correct. If you encounter issues, make sure that the JSON is well-formed. If the response is actually a string, you might need to remove any unnecessary quotes surrounding the JSON string before parsing it.
If you’re still facing issues, please provide more details or code snippets, and I’ll do my best to assist you.
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: