-2

Hello I have below json in api:

{
   "data":[
      {
         "id":"297968",
         "type":"email_from_class",
         "extra":[

         ],
         "sessionid":null,
         "canBeDeleted":true
      },
      {
         "id":"297919",
         "type":"microsite_email_opened_class",
         "extra":{
            "Prescribing Information":[
               {
                  "id":"599",
                  "name":"abc",
                  "type":"pdf",
                  "description":"abx",
                  "subtitle":"PI",
                  "content":"",
                  "thumb":""
               }
            ],
            "emailinfo":{
               "from":"",
               "to":"",
               "emailSentDate":"",
               "opened":[

               ]
            }
         },
         "sessionid":null,
         "canBeDeleted":true
      },
      {
         "id":"297848",
         "type":"microsite_email_class",
         "extra":{
            "Prescribing Information":[
               {
                  "id":"599",
                  "name":"",
                  "type":"",
                  "description":"",
                  "subtitle":"",
                  "content":"",
                  "thumb":""
               }
            ],
            "emailinfo":{
               "from":"",
               "to":"",
               "emailSentDate":"",
               "opened":[

               ]
            }
         },
         "sessionid":null,
         "canBeDeleted":true
      },
      {
         "id":"297847",
         "type":"sales_visit_class",
         "extra":[
            {
               "duration":"",
               "title":"New presentation started",
               "section":"",
               "hasrating":-1,
               "thumb":null,
               "extra":null
            },
            {
               "duration":28,
               "title":"Surveys",
               "section":null,
               "hasrating":-1,
               "thumb":null,
               "extra":null
            }
         ],
         "sessionid":"18540676-30bc-4bbc-bf72-c803f8e360b3",
         "canBeDeleted":true
      },
      {
         "id":"297811",
         "type":"email_open_class",
         "extra":{
            "from":"",
            "to":"",
            "cc":"",
            "bcc":"",
            "emailSentDate":"",
            "opened":[
               1561040954
            ],
            "locationinfo":{
               "1561040954":{
                  "ip":"",
                  "country":"",
                  "region":"",
                  "city":"",
                  "coordinates":"",
                  "mobile":false,
                  "browser_name":"",
                  "browser_version":"",
                  "platform":""
               }
            }
         },
         "sessionid":null,
         "canBeDeleted":true
      },
      {
         "id":"297808",
         "type":"email_to_class",
         "extra":{
            "from":"",
            "to":"",
            "cc":"",
            "bcc":"",
            "emailSentDate":"",
            "opened":[
               1561040954
            ]
         },
         "sessionid":null,
         "canBeDeleted":true
      }
   ],
   "indicators":{
      "total_activities":6
   }
}

In above json extra key's value is not static it has unstructured data so how can I parse it ?

Any help would be appriciated.

3

1 Answer 1

4

The fact that "type" is one of the keys - and is also a reserved keyword - tells you that yes you need to use JSON.deserializeUntyped anyway as Pranay suggested.

If a section contains unstructured data, you need to figure out what possible data structures you are willing to extract data from and then write Apex code to handle extracting them.

In general deserializeUntyped returns a Map<String, Object>. Each of the objects in the map can be either a primitive data type (e.g. String, Integer, Decimal, Boolean) or a List<Object> or another Map<String,Object>. You can test which one it is using instanceof on the objects, and then you have to cast it to the correct type in order to manipulate or inspect the data further. And then if it's one of the latter two types, you have to again take the objects inside of it and test/cast them.

2
  • One could work around type being a reserved word by replacing the json "type": with "type_x": and deserializing into a custom apex type - the real problem is that extra is either an array or a custom type(s) and deserializing into a top level custom apex type that includes a property defined as Object is not supported (sad) so as you say, Json.deserializedUntyped is the way to go.
    – cropredy
    Jul 17, 2019 at 21:36
  • Indeed. It would be lovely if I could just include a Map<String,Object> or Object[] in the type I deserialize to, and have it behave just like deserializeUntyped, but alas no.
    – Charles T
    Jul 17, 2019 at 22:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.