I create a record in one system and that creates the similar record in another and returns the corresponding ID from the destination system which I would to stamp in my source. Deserialized the response to a map of string,string :

Map<String,String> mapresp = (Map<String,String>)JSON.deserialize(resp.getbody(),Map<String, string>.class);

and below is the result


How do I get to the value of FieldId__c?

1 Answer 1


I don't think you'd be able to do that with deserializing to a Map<String, String> like you currently are.

You could use JSON.deserializeUntyped() and work with a Map<String, Object> to be able to dive deeper into your deserialized json, but since you're working with an SObject (even if it's from a different org), it'd be easier to just deserialize it as an Account.

String myJson = '{"attributes":{"type":"Account","url":"/services/data/v47.0/sobjects/Account/0014T000005aLUrQAM"},"FieldId__c":"0010I00002Kyu2zQAB","Id":"0014T000005aLUrQAM"}}';

// The second argument to deserialize() tells it which object/class to expect
// It's going to be <SObject name, or Apex class name>.class
// The return type of deserialize() is always "Object", even though we told it what
//   class to use to do the deserialization.
// Because of that, we also need to explicitly type-cast the result
Account acct = (Account)JSON.deserialize(myJson, Account.class);

You'd then access your target value the same way that you'd access any other field on an SObject, acct.FieldId__c.

This does assume that your Account object has a FieldId__c field (that you have at least read access to) in the org that you're running this code in.

As long as you don't try to insert the deserialized account (the Id won't be valid in both the source and target orgs), and keep it strictly in memory, then things should work out fine.

  • Hi Derek, The value in FieldId__c is the ID of the source org and the value that I receive in ID is the value from the destination org.So, would need to query for FieldId__c = Id in this org to fetch the record. Modified the code per below, but I get an error that variable does not exist, which is obvious. But how do I get the value of FieldId__c? Account acc= (Account)JSON.deserialize(myJson, account.class); for(Account a : [select CustomId__c from account where Id=:acc.FieldId__c]) }
    – Priya
    Nov 27, 2019 at 18:45
  • Also, my JSON response has the additional ID value prior to the attributes like - {0014T000005aLUrQAM={"attributes":{"type":"Account","url":"/services/data/v47.0/sobjects/Account/0014T000005aLUrQAM"},"FieldId__c":"0010I00002Kyu2zQAB","Id":"0014T000005aLUrQAM"}} So the string value doesn't really begin from "attributes". Is there a way to not include this "0014T000005aLUrQAM="?
    – Priya
    Nov 27, 2019 at 18:46
  • @Priya When you system.debug() a Map, it gives you the output as key=value. If that were truly included in the response body, you wouldn't be able to deserialize using the JSON class (because key=value is not valid JSON). If you were to debug the response body, I think you'd find it doesn't contain 0014T000005aLUrQAM= anywhere. Also, again, my solution assumes that you have FieldId__c defined on your target object (Account) in both orgs. You might want to edit your question to add more detail if my solution doesn't work for you.
    – Derek F
    Nov 27, 2019 at 19:12
  • Hi Derek, this is the response. I've added additional details to the question if(resp.getstatuscode()==200){ system.debug('Response:' + resp.getbody()); //debug --Response:{"0014T000005aTXUQA2":"{\"attributes\":{\"type\":\"Account\",\"url\":\"/services/data/v47.0/sobjects/Account/0014T000005aTXUQA2\"},\"FieldId__c\":\"0010I00002KzuDxQAJ\",\"Id\":\"0014T000005aTXUQA2\"}"}//
    – Priya
    Nov 28, 2019 at 11:06
  • Map<String,String> mapresp = (Map<String,String>)JSON.deserialize(resp.getbody(),Map<String, string>.class); system.debug('Mapresp:' + mapresp); //debug {0014T000005aTXUQA2={"attributes":{"type":"Account","url":"/services/data/v47.0/sobjects/Account/0014T000005aTXUQA2"},"FieldId__c":"0010I00002KzuDxQAJ","Id":"0014T000005aTXUQA2"}} Account acc = (Account)JSON.deserialize(??,Account.class);
    – Priya
    Nov 28, 2019 at 11:06

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.