11

I have some JSON being returned, but getting the values deeply nested is very annoying.

{  
"Result":"1",
"Matches":[  
  {  
     "BioInformation":{  
        "BirthDate":"1987-09-13T05:00:00",
        "SSN":"123456789",
        "Sex":"M"
        "UserID":"116713"
     },
     "IdInformation":{  
        "ChangeIndicator":null,
        "DataOrigin":"WebServices",
        "EntityIndicator":"P",
        "FirstName":"Test",
        "mID":"921821621",
        "LastName":"B",
        "MiddleName":null,
        "RowID":"AAAYGXBBAXAF8mZAA6",
        "SurnamePrefix":null,
        "UserID":null
     },
     "UniqueId":"AAg9eAAACAAA9eBAAA"
  },
  {  
     "BioInformation":{  
        "ConfidInd":false,
        "SSN":"123456789",
        "Sex":"N",
        "UserID":"155064"
     },
     "IdInformation":{  
        "ChangeIndicator":null,
        "DataOrigin":"WebServices",
        "EntityIndicator":"P",
        "FirstName":"Steven",
        "mID":"921821721",
        "LastName":"Johnson",
        "MiddleName":null,
        "RowID":"AADFAAKFJAA3dZDD3",
        "SurnamePrefix":null,
        "UserID":"TEST123"
     },
     "UniqueId":"AAg9eAAACAAA9eBAAB"
  }
 ]
}

I have something like:

Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(body); 
List<Object> matches = (List<Object>) m.get('Matches');

if (matches != null) {
    for (Object match : matches) {
        for (Object data : match) {

         }
         System.debug('match : ' + match);
    }      
}

Is there a good/easy/efficient way to access the inside information such as BirthDate, RowID, etc?

  • 2
    If you take your JSON over to json2apex it can build a class for you to make serialization/deserialization a snap. – sfdcfox Dec 8 '17 at 20:33
  • I also love JSON2APEX but it sometimes gets flustered by protected field names, and ends up producing the long hand version of the parsing code, even if the field in question isn't one you need. So look out for that, and consider editing the JSON first to remove any problematic field names – BritishBoyinDC Nov 26 '19 at 0:26
10

Use serialization/deserialization and it's quite easy. You can generate the definitions using JSON2Apex, but it's not rocket surgery and I typically prefer to do it by hand. For example you might do:

public class MyPayload
{
    public class Result
    {
        public final String uniqueId;
        public final IdInformation idInformation;
        public final BioInformation bioInformation;
    }
    public class BioInformation
    {
        final Boolean confidInd;
        final String ssn, sex, userId;
    }
    public class IdInformation
    {
        final String changeIndicator, dataOrigin, entityIndicator, mID, rowId, userId;
        final String firstName, lastName, middleName, surnamePrefix;
    }
}

Then it's easy to work with the results.

List<MyPayload.Result> results = (List<MyPayload.Result>)JSON.deserialize(
    somePayload, List<MyPayload.Result>.class
);
for (MyPayload.Result result : results)
{
    system.debug(result.idInformation.firstName);
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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