function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nithya S 1Nithya S 1 

parse json response

Hi all,
Below is the response I receive. How can I get the value of Field__c?
Response:{"Account":"{\"attributes\":{\"type\":\"Account\",\"url\":\"/services/data/v47.0/sobjects/Account/0014T000005aUCBQA2\"},\"Field__c\":\"0010I00002L0OtaQAF\",\"Id\":\"0014T000005aUCBQA2\"}"}

When I hardcode the response as a string, then I can navigate to Field__c. But how do I convert this response body to a string in the format as String myJson = '{"Account":{"attributes":{"type":"Account","url":"/services/data/v47.0/sobjects/Account/0014T000005aRZAQA2"},"Field__c":"0010I00002KzV6mQAF","Id":"0014T000005aRZAQA2"}}';
Danish HodaDanish Hoda
Hi Nithya,
Please refer below context:
string str = '{"Account":"{\"attributes\":{\"type\":\"Account\",\"url\":\"/services/data/v47.0/sobjects/Account/0014T000005aUCBQA2\"},\"Field__c\":\"0010I00002L0OtaQAF\",\"Id\":\"0014T000005aUCBQA2\"}"}';
Map<String, Object> m1 = (Map<String, Object>)JSON.deserializeUntyped(str.substringAfter(':"'));
system.debug('-->> '+m1.get('Field__c'));

 
Nithya S 1Nithya S 1
Hi Danish, thank you for your response. But I would want to know how I can convert the response to a string. I mean to..add "'" (the inverted comma) at the beginning and end of the response body. Thanks!
Nithya S 1Nithya S 1
To add, Map<String, String> tosend = new Map<String,String>(); for(Account objAccount :[SELECT FieldId__c,Id FROM Account WHERE Id=:acc.Id]){ tosend.put('Account',JSON.serialize(objAccount)); }
This is how the response is being received..