0

I try to parse JSON below with java Jackson library but I not able to get the value at level "ChannelType", "Address" and etc. It is because of value "endpoint123" & "endppoint456", which cause some problem for me. I try to google but can't find elsewhere. Would like some 1 expert here to assist. Thanks in advance.

    {
    "Message": {},
    "ApplicationId": "aaa",
    "CampaignId": "bbb",
    "TreatmentId": "0",
    "ActivityId": "ccc",
    "ScheduledTime": "2021-04-07T08:43:16.406Z",
    "Endpoints": {
        "endpointa123": {
            "ChannelType": "CUSTOM",
            "Address": "bobby@abc.com.my",
            "EndpointStatus": "ACTIVE",
            "OptOut": "NONE",
            "EffectiveDate": "2021-04-07T08:35:48.796Z",
            "User": {
                "UserAttributes": {
                    "Custom4": [
                        "data4"
                    ],
                    "Custom3": [
                        "data3"
                    ],
                    "Custom2": [
                        "data2"
                    ],
                    "Custom1": [
                        "data1"
                    ],
                    "Custom5": [
                        "data5"
                    ]
                }
            },
            "CreationDate": "2021-04-07T08:35:48.796Z"
        },
        "endpoint456": {
            "ChannelType": "CUSTOM",
            "Address": "fifis@abc.com.my",
            "EndpointStatus": "ACTIVE",
            "OptOut": "NONE",
            "EffectiveDate": "2021-04-07T08:35:48.792Z",
            "User": {
                "UserAttributes": {
                    "Custom4": [
                        "data4"
                    ],
                    "Custom3": [
                        "data3"
                    ],
                    "Custom2": [
                        "data2"
                    ],
                    "Custom1": [
                        "data1"
                    ],
                    "Custom5": [
                        "data5"
                    ]
                }
            },
            "CreationDate": "2021-04-07T08:35:48.792Z"
        }
    }
}

My code as per below, Campaign is my own define class.

            Campaign campaign = mapper.readValue(json, Campaign.class);
            String valueOut =  mapper.writerWithDefaultPrettyPrinter().writeValueAsString(campaign);
            System.out.println(valueOut);

My Java Object Model for the above as per below.

Campaign 
   |--EndPoints
       |-- EndPointsContainer (Array) 
            |--User 
                 |--UserAttributes

Following is result which I get.

"Message" : { },
"ApplicationId" : "aaa",
"CampaignId" : "bbb",
"TreatmentId" : "0",
"ActivityId" : "ccc",
"ScheduledTime" : "2021-04-07T10:44:27.598Z",
"EndPoints" : {
"endPointsContainer" : [ ],
"creationDate" : null,
"CreationDate" : null

Thanks for @Robert. It at least quite similar with original JSON. POJO was reconstruct as per below.

Campaign 
   |--EndPoints
        |--User 
             |--UserAttributes

However, CreationDate is inside object "endpoint123" and "endpoint456", instead of outside. Because it is lengthy. I posted part of it.

"ScheduledTime": "2021-04-07T08:43:16.406Z",
"Endpoints": {
    "endpointa123": {
        "CreationDate": "2021-04-07T08:35:48.796Z"
        "ChannelType": "CUSTOM",
Sean
  • 1
  • 1
  • How does your object look like? It seems you need a `Map` and also, the result you've posted is not a valid JSON – Yassin Hajaj Apr 07 '21 at 11:17
  • 1
    In your sample data there are no arrays. `Endpoints` seems to be a `Map`. – Robert Apr 07 '21 at 11:17
  • @Robert, thanks for your reply. At least it is quite same as original JSON. I remove "EndPointsContainer (Array)" from my model. Move all json properties in EndPointsContainer to EndPoints. Define Map as per your suggestion. Only 1 thing. "Creation Date" was inside object "endpoint123" and "endpoint456" instead of outside. – Sean Apr 08 '21 at 02:44

1 Answers1

0

There are two problems here,

  1. The JSON is not in correct format. If you expect the enpoints to be an array and that to be with variable name endPointsContainer, you need to have same name. endpointa123 and endpoint456 will be treated as different objects and wont be part of single list
  2. Variable CreationDate is part of endpoint and hence it will available inside the endPointsContainer

So the correct JSON as per your expectation would be,

{
  "Message": {},
  "ApplicationId": "aaa",
  "CampaignId": "bbb",
  "TreatmentId": "0",
  "ActivityId": "ccc",
  "ScheduledTime": "2021-04-07T08:43:16.406Z",
  "Endpoints": {
    "endPointsContainer": [
      {
        "endpoint": {
          "ChannelType": "CUSTOM",
          "Address": "bobby@abc.com.my",
          "EndpointStatus": "ACTIVE",
          "OptOut": "NONE",
          "EffectiveDate": "2021-04-07T08:35:48.796Z",
          "User": {
            "UserAttributes": {
              "Custom4": [
                "data4"
              ],
              "Custom3": [
                "data3"
              ],
              "Custom2": [
                "data2"
              ],
              "Custom1": [
                "data1"
              ],
              "Custom5": [
                "data5"
              ]
            }
          },
          "CreationDate": "2021-04-07T08:35:48.796Z"
        }
      },
      {
        "endpoint": {
          "ChannelType": "CUSTOM",
          "Address": "fifis@abc.com.my",
          "EndpointStatus": "ACTIVE",
          "OptOut": "NONE",
          "EffectiveDate": "2021-04-07T08:35:48.792Z",
          "User": {
            "UserAttributes": {
              "Custom4": [
                "data4"
              ],
              "Custom3": [
                "data3"
              ],
              "Custom2": [
                "data2"
              ],
              "Custom1": [
                "data1"
              ],
              "Custom5": [
                "data5"
              ]
            }
          },
          "CreationDate": "2021-04-07T08:35:48.792Z"
        }
      }
    ]
  }
}

When I deserialized this in to MAP class,

ObjectMapper jsonMapper = new ObjectMapper();
Map<String,Object> compaign = jsonMapper.readValue(json, Map.class);

this is how it looks like,

enter image description here

pratap
  • 538
  • 1
  • 5
  • 11
  • thanks for your assist. However, I not able to modify this JSON. This is original JSON generated by AWS Pinpoint. I can't modify anything on the JSON generated by AWS Pinpoint. – Sean Apr 08 '21 at 02:50
  • @Sean you need to change the object structure. You wont be able to get the endPoint array since they have different names. They will be treated as different objects. I suggest you to check this thread https://stackoverflow.com/questions/1957406/generate-java-class-from-json. this will help you to generate POJO using the JSON – pratap Apr 08 '21 at 05:03