6

I have a JSON string:

{addressDetails=({addressId=56941, addressLine1=76 Kilaben Rd, addressLine2=null, addressLine3=null, addressType=H, countryCode=AUS, locality=KILABEN BAY, postcode=2283, sortplan=017, state=NSW, ...}), aliasName=null, concessionInfo=null, contactDate=null, contactDetails=({contactDetail=, contactId=122827, contactType=H, delta=2}), contactPreference={correspondanceLevel=0, correspondanceLevelDescription=null, emailOpt=N, smsOpt=N}, deceasedFlag=N, dob=1952-07-29T00:00:00, firstContactDate=null, firstName=John, ...}

I'm mapping this using:

Map<String, Object> meta = (Map<String, Object>) JSON.deserializeUntyped(viewPersonValue);

To get firstname I'd say:

meta.get('firstName');

The issue I'm facing is accessing the values in addressDetails.

I've tried addressDetails[0] and addressDetails.addressId but I think I'm approaching this wrong.

Any help would be greatly appreciated.

2 Answers 2

9

You are really working uphill by doing this all untyped, but you can get the value you want this way:

list<Object> l = (list<Object>) meta.get('addressDetails');
map<String,Object> m = (map<String,Object>) l[0];
Integer i = (Integer) m.get('addressId');

Honestly, if you make use of the Apex type system, you will have an easier time working with the data. If you create a class for the incoming data structure, abbreviated as:

public class Meta{
   public list<addressDetail> addressDetails {get; set;} 

   public class addressDetail{
       public Integer addressId {get; set;}
   }   
}

you will be able to access the data from your JSON strings as so:

Meta MyMeta = (Meta) JSON.deserialize(viewPersonValue,Meta.class);
system.debug(MyMeta.addressDetails[0].addressId);
2
  • Hi Martin, thanks for the response but when I say: system.debug(meta.get('addressDetails')[0].get('addressId')); I'm getting Error: Compile Error: Expression must be a list type: Object at line 79 column 23
    – Corey
    May 18, 2017 at 6:30
  • Are you able to quickly explain how you would convert the map to string to prevent further issues? Thank you so much as well for your help :)
    – Corey
    May 18, 2017 at 6:49
2

May be this simple example can help

string viewPersonValue = '{"name":"John",
                            "age":30,
                            "cars": 
                                  {"car1":"Ford",
                                   "car2":"BMW",
                                   "car3":"Fiat"}
                          }';

Map<String, Object> meta = (Map<String, Object> JSON.deserializeUntyped(viewPersonValue);
Map<String, Object>  carMap = (Map<String, Object>) meta.get('cars');
system.debug(carMap.get('car1'));
1
  • Getting this error: Invalid conversion from runtime type List<ANY> to Map<String,ANY> Error is in expression '{!parseNew}' in page Find_Person: Class.findPerson.parseNew: line 79, column 1 This is my code: Map<String, Object> meta = (Map<String, Object>) JSON.deserializeUntyped(viewPersonValue); Map<String, Object> addressDetailsMap = (Map<String, Object>) meta.get('addressDetails'); system.debug(addressDetailsMap.get('addressId'));
    – Corey
    May 18, 2017 at 6:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .