skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. The simplest way for Jackson to access your variables is to provide getters like the following:

    @JsonIgnoreProperties(ignoreUnknown = true)
    @Getter  //option 1
    @AllArgsConstructor
    @NoArgsConstructor
    public class MyPojo {
        private String this_value;
        private String other_value;
        
    
        @Override
        public String toString() {
            return "MyPojo{" +
                    "this_value='" + this_value + ''' +
                    ", other_value='" + other_value + ''' +
                    '}';
        }
    }
    
    @JsonIgnoreProperties(ignoreUnknown = true)
    @AllArgsConstructor
    @NoArgsConstructor
    public class MyPojo {
        private String this_value;
        private String other_value;
    
        public String getThis_value() {  //option 2
            return this.this_value;
        }
    
        public String getOther_value() {
            return this.other_value;
        }
    
        @Override
        public String toString() {
            return "MyPojo{" +
                    "this_value='" + this_value + ''' +
                    ", other_value='" + other_value + ''' +
                    '}';
        }
    }
    

    Alternatively, You need to use both @JsonProperty and @JsonCreator in conjunction like so:

    @JsonIgnoreProperties(ignoreUnknown = true)
    public class MyPojo {
        private String this_value;
        private String other_value;
    
        @JsonCreator
        public MyPojo(@JsonProperty("this_value") String this_value, @JsonProperty("other_value") String other_value) {
            this.this_value = this_value;
            this.other_value = other_value;
        }
    
        @Override
        public String toString() {
            return "MyPojo{" +
                    "this_value='" + this_value + ''' +
                    ", other_value='" + other_value + ''' +
                    '}';
        }
    }
    

    Running the following code:

    public class Main {
        public static void main(String[] args) throws Exception {
            final var mapper = new ObjectMapper();
            final var json ="{"this_value":"this","other_value":"other"}";
            System.out.println(json);
            System.out.println(mapper.readValue(json, MyPojo.class));
        }
    }
    

    will give an output of:

    {"this_value":"this","other_value":"other"}
    MyPojo{this_value='this', other_value='other'}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search