4

Im getting response from external api

"success": true,
    "data": [
        {}

I'd like to map only data and it's corresponding array as entire class. Right now I have wrapper for it but it is +1 class just for that.

public class YYYYYY {

    private boolean success;
    @JsonProperty(value = "data")
    private List<PipeDriveContact> arrayData;
filemonczyk
  • 125
  • 6
  • Create a filter in web.xml` remove all unwanted attributes from request and put the remained one back into the request and in controller you can take as `@RequestBody YYYYYY wrapperObj` – Vipin CP Oct 27 '17 at 07:51

4 Answers4

1

This is similar to https://stackoverflow.com/a/19097149/6785908

You'll first need to get the array

String jsonStr = "{\"success\": true,\"data\": [{\"test\": \"some data\"}]}";
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(jsonStr);
ArrayNode arrayNode = (ArrayNode) node.get("data");
System.out.println(arrayNode);
List<PipeDriveContact> pojos = mapper.readValue(arrayNode.toString(), new TypeReference<List<PipeDriveContact>>() {});

System.out.println(pojos);

prints (with a toString())

[{\"test\": \"some data\"}] // the json array 

But trust me, unless you have a very compelling reason (than "I don't want one more class"), I would discourage you from heading down this path, instead implement it with the wrapper class and call it done. Reason: In future you may generate your Pojos from a contract (swagger spec / ol JSON schema), or you may find some use for the "success" field.

so-random-dude
  • 15,277
  • 10
  • 68
  • 113
0

Either you can use @JsonCreator for defining constructor and populate only data

Or you can also use Custom de-serializer too.

Further reading and examples:

  1. http://www.baeldung.com/jackson-annotations
  2. http://buraktas.com/convert-objects-to-from-json-by-jackson-example/
  3. https://codexplo.wordpress.com/2015/11/26/custom-json-deserialization-with-jackson/
Yogi
  • 1,805
  • 13
  • 24
0

Maybe it's not exactly what you are looking for, but you can map only the fields you need in your class with @JsonIgnoreProperties(ignoreUnknown = true) :

@JsonIgnoreProperties(ignoreUnknown = true)
public class YYYYYY {

@JsonProperty(value = "data")
private List<PipeDriveContact> arrayData;
senerh
  • 1,315
  • 10
  • 20
0

If you absolutely don't need the other keys in the outermost object, you could parse out the array against the key "data" and then parse it separately into your POJO. Following is my rough implementation:

First, parse out the data array:

String json = "{\"success\": true,\"data\": [{\"test\": \"some data\"}]}";

JSONObject obj = new JSONObject(json);
String data = obj.getJSONArray("data").toString();

Then, using Jackson (or anything else), create an ArrayList with your required objects:

ObjectMapper objectMapper = new ObjectMapper();
TypeReference<ArrayList<PipeDriveContact>> typeRef = new TypeReference<ArrayList<PipeDriveContact>>() {};
ArrayList<PipeDriveContact> dataArray = objectMapper.readValue(data, typeRef);

Following is the model POJO I created for testing:

public class PipeDriveContact {
    private String test;

    public String getTest() { return test; }

    public void setTest(String test) { this.test = test; }
}

Following are the dependencies I used:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.3</version>
</dependency>
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20171018</version>
</dependency>

Hope this helps.

Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58