1

I have the following JSON schema from the response of an API which can either be :

{
  "meta": {
      "someData": " "
  },
  "A": [
     {
       "code": 123,
       "id": "string",
       "data": {} 
     },
     {
       "code": 123,
       "id": "string",
       "data": {} 
     }
  ]
}

OR

{
  "meta": {
      "someData": " "
  },
  "B": [
     {
       "code": 123,
       "id": "string",
       "data": {} 
     },
     {
       "code": 123,
       "id": "string",
       "data": {} 
     }
  ]
}

How do I get obtain just the date from the list or either property A or property B ?

I expect output to be something like:

A[0].data = {}
A[1].data = {}

OR

B[0].data = {}
B[1].data = {}

And I want to store the data property, which is an object with variable number of sub properties in a map.

Tejus
  • 101
  • 1
  • 4

1 Answers1

-1

You should be able to use jackson's object mapper. You should create a class or interface to define your json object as well. The A or B may be an issue. You might be able to create a class/interface for each.

final ObjectMapper objectMapper = new ObjectMapper();
final CallbackRequest callbackRequest = objectMapper.readValue(json, CallbackRequest.class);

Also, here is an example: https://github.com/NikhilShah1647/jackson-example/blob/master/src/main/java/com/journaldev/jackson/json/JacksonObjectMapperExample.java

And here is an example of deserializing a json object with possible multiple types: Jackson deserialization of type with different objects

SSwayney
  • 1
  • 2