Q:

How to parse unstructured JSON in APEX

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.

A:

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.

