Java get nested value from ResponseEntity without creating a pojo

805 Views Asked by At

I am trying to get a single nested value from a ResponseEntity but I am trying to do so without having to create a pojo for every possible item as this is a third party api response.

Example response.getBody() as it appears in Postman:

{
    "message": "2 records found",
    "records": [
        {
            "Account": {
                "Id": "1",
                "Name": "Foo Inc"
            },
            "CaseNumber": "200",
            "Contact": {
                "FirstName": "Foo",
                "LastName": "Bar"
            },
            "Status": "In Progress",
            "StatusMessage": "We are working on this."
        },
        {
            "Account": {
                 "Id": "1",
                 "Name": "Foo Inc"
            },
            "CaseNumber": "100",
            "Contact": {
                "FirstName": "Foo",
                "LastName": "Bar"
            },
            "Status": "Closed"
        }

    ]
}

Basically, if I were in JS, I am looking for:

for(let record of res.body.records){
   if(record && record.CaseNumber === "200"){
      console.log(record.Status)
}
res.body.records[0].Status

Currently, they are are doing this to check if the response is empty:

ResponseEntity<Object> response = restTemplate.exchange(sfdcURL, HttpMethod.POST, entity, Object.class);

LinkedHashMap<Object, Object> resMap = (LinkedHashMap<Object, Object>) response.getBody();
        
List<Object> recordsList = (List<Object>) resMap.get("records");

if (recordsList.size() <= 0) { return error }

But I need to get the value of of "Status" and I need to do so without creating a pojo.

I appreciate any guidance on how I can do this in Java

UPDATE

So the response.getBody() is returned and when it is displayed in Postman, it looks like the pretty JSON shown above. However, when I do:

System.out.println(response.getBody().toString())

it looks like:

{message=2 Records Found, records=[{Account={Id=1, Name=Foo Inc}, CaseNumber=200, Contact={FirstName=Foo, LastName=Bar}, //etc

To make it worse, one of the fields appears in the console as follows (including linebreaks):

[...], Status=In Progress, LastEmail=From: [email protected]
Sent: 2022-08-08 10:14:54
To: [email protected]
Subject: apropos case #200


Hello Foo,
We are working on your case and stuff

Thank you,
us, StatusMessage=We are working on this., OtherFields=blah, [...]

text.replaceAll("=", ":") would help some, but won't add quotations marks nor would it help separate that email block.

How can I so that the responses here like ObjectMapper and JSONObject can work?

5

There are 5 best solutions below

0
On BEST ANSWER

As Oliver suggested JsonNode seems to be the best approach. But, if I receive the ResponseEntity<Object>, I still cannot figure out a way to convert it to readable Json (and thus convert it to JsonNode), so I am still open to responses for that part.

I was able to get it to work by changing the ResponseEntity<Object> to ResponseEntity<JsonNode> so this is what I will be submitting for now:

ResponseEntity<JsonNode> response = restTemplate.exchange(sfdcURL,
      HttpMethod.POST, entity, JsonNode.class);

JsonNode records = response.getBody().get("records");

String status = null;
String statusMessage = null;

for (JsonNode rec : records) {
    if(rec.get("CaseNumber").asText().equals(caseNumber)) {
        status = rec.get("Status").asText();
        if(rec.has("StatusMessage")) {
            statusMessage = rec.get("StatusMessage").asText();
        }
    } else {
        statusMessage = "Invalid CaseNumber";
    }
}

Because the overall method returns a ResponseEntity<Object> I then converted my strings to a HashMap and returned that:

HashMap<String, String> resMap = new HashMap<String, String>();
resMap.put("Status", status);
resMap.put("StatusMessage", statusMessage);
        
return new ResponseEntity<>(resMap, HttpStatus.OK);

This is not a perfect solution, but it works for now. Would still be better for exception handling if I could receive a ResponseEntity<Object> and then convert it to a JsonNode though. Thanks everyone for the responses!

1
On

So the response.getBody() is returned and when it is displayed in Postman, it looks like the pretty JSON shown above. However, when I do:

...

text.replaceAll("=", ":") would help some, but won't add quotations marks nor would it help separate that email block.

How can I so that the responses here like ObjectMapper and JSONObject can work?

Firstly, Jackson is the default message converter which Spring Web uses under the hood to serialize and deserialize JSON. You don't need to introduce any dependencies.

Secondly, the process serialization/deserialization is handled by the framework automatically, so that in many cases you don't need to deal with the ObjectMapper yourself.

To emphasize, I'll repeat: in most of the cases in Spring you don't need to handle raw JSON yourself. And in the body of ResponseEntiry<Object> produced by the method RestTemplate.exchange() you have a LinkedHashMap in the guise of Object, it's not a raw JSON (if you want to know why it is a LinkedHashMap, well because that's how Jackson stores information, and it's a subclass of Object like any other class in Java). And sure, when you're invoking toString() on any implementation of the Map you'll get = between a Key and a Value.

So, the problem you've mentioned in the updated question is artificial.

If you want to deal with a Map instead of an object with properly typed properties and here's how you can do that:

RestTemplate restTemplate = new RestTemplate();
        
ResponseEntity<LinkedHashMap<String, Object>> response = restTemplate.exchange(
    sfdcURL, HttpMethod.POST, entity, new ParameterizedTypeReference<>() {}
);
    
Map<String, Object> resMap = response.getBody();
    
List<Object> recordsList = (List<Object>) resMap.get("records");
    
if (recordsList.isEmpty()) { ... }

If there are redundant lines in the Values which you want to trim, then as a remedy you can introduce a custom Jackson-module declaring a Deserializer which would handle leading/trailing white-space and new lines, described in this answer. Deserialize in the module would be applied by default, other options would require creating classes representing domain objects which you for some reasons want to avoid.

1
On

PLEASE DO NOT use Genson as Hiran showed in his example. The library hasn't been updated since 2019 and has many vulnerable dependencies!

Use Jackson or Gson.

Here how you can serialize a string into a Jackson JsonNode:

ObjectMapper mapper = new ObjectMapper();

String json = ...;
JsonNode node = mapper.readTree(json);

If you want to serialize a JSON object string into a Map:

ObjectMapper mapper = new ObjectMapper();

String json = ...;
Map<String, Object> map = mapper.readValue(json, HashMap.class);

You can read more about JsonNode here and a tutorial here.

1
On

You can use JSON-Java library and your code will look like this:

JSONObject jsonObject = new JSONObject(JSON_STRING);
String status = jsonObject.getJSONArray("records")
                .getJSONObject(0)
                .getString("Status");
System.out.println(status);

Or in a loop

JSONArray jsonArray = new JSONObject(jsonString).getJSONArray("records");
for(int i =0; i < jsonArray.length(); i++) {
    String status = jsonArray
                .getJSONObject(i)
                .getString("Status");
    System.out.println(status);
}
3
On

You can either convert the string to valid json (not that trivial) and deserialise into a Map<String, Object>, or just pluck the value out of the raw string using regex:

String statusOfCaseNumber200 = response.getBody().toString()
  .replaceAll(".*CaseNumber=200\\b.*?\\bStatus=([^,}]*).*", "$1");

This matches the whole string, captures the desired status value then replaces with the status, effectively "extracting" it.

The regex:

  • .*CaseNumber=200\b everything up to and including CaseNumber=200 (not matching longer numbers like 2001)
  • .*? as few chars as possible
  • \\bStatus= "Status=" without any preceding word chars
  • ([^,}]*) non comma/curly brace characters
  • .* the rest

It's not bulletproof, but it will probably work for your use case so it doesn't need to be bulletproof.


Some test code:

String body = "{message=2 Records Found, records=[{Account={Id=1, Name=Foo Inc}, CaseNumber=200, Contact={FirstName=Foo, LastName=Bar}, Status=In Progress, StatusMessage=We are working on this.}, {Account={Id=1, Name=Foo Inc}, CaseNumber=100, Contact={FirstName=Foo, LastName=Bar}, Status=Closed}]";
String statusOfCaseNumber200 = body.replaceAll(".*CaseNumber=200\\b.*?\\bStatus=([^,}]*).*", "$1");
System.out.println(statusOfCaseNumber200); // "In Progress"