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
Shubham Sinha 56Shubham Sinha 56 

how to use JSON output

Hello,

I have one scenarion where I am getting output in JSON format . 
JSON - {"SystemModstamp":{"oldValue":"2021-06-10T08:32:25.000Z","value":"2021-06-10T09:03:24.000Z"},"Status":{"oldValue":"New","value":"Accept"}}

I want to use this output in IF condition in my JS of lightning component. Like if Status Value is' Accept' then do my logic.
P.S.  SystemModstamp, Status both are fields of Lead.

Please help me on this. Thanks in advance
ShivankurShivankur (Salesforce Developers) 
Hi Shubham,

You could use Apex code like below to get the parsed values from json and then use it accordingly by storing in some variable, Here is sample code for your understanding:
List<Object> fieldList = (List<Object>)JSON.deserializeUntyped('[{"oldValue":"2021-06-10T08:32:25.000Z","value":"2021-06-10T09:03:24.000Z"},{"oldValue":"New","value":"Accept"}]');
for(Object fld : fieldList){    
    Map<String,Object> data = (Map<String,Object>)fld;
    
    //Magic!
    system.debug(data.get('value'));
}
Also, we have JSON to Apex application(https://json2apex.herokuapp.com/) where the response can be converted to get the relevant apex and then you can process the response according to the standard documentation as mentioned in below links:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_json_jsonparser.htm

Some more helpful links:
https://intellipaat.com/community/19062/multi-level-nested-json-parsing-with-apex-salesforce
https://developer.salesforce.com/forums/?id=9060G000000I17RQAS

Hope above information helps. Please mark as Best Answer so that it can help others in future.

Thanks.
 
Maharajan CMaharajan C
Hi Shubham,

I don't know you are using the LWC or Aura.

But you can try like below :

You no need to declare JSON string for testing purpose i have created

var jsonstr = '{"SystemModstamp":{"oldValue":"2021-06-10T08:32:25.000Z","value":"2021-06-10T09:03:24.000Z"},"Status":{"oldValue":"New","value":"Accept"}}';

var jStr = JSON.parse(jsonstr);

console.log('jsonstr --> ' + JSON.parse(jsonstr));

var Acceptbool = false;

for(var key in jStr){
   if(key == 'Status'){
     console.log('***' + jStr[key].value);
     if(jStr[key].value == 'Accept'){
       Acceptbool = true;
     }
   }
}

console.log('Acceptbool --> ' + Acceptbool );

if(Acceptbool){
   //  do your logic
}

Thanks,
Maharajan.C

 
Suraj Tripathi 47Suraj Tripathi 47
Hi Shubham,
Greetings!

There are two ways to handle this JSON.
First, create a wrapper class with these variables to directly deserialize JSON using your wrapper class.
Second, Please use deserializeUntyped to break this JSON into Map where the key is String and Value is Object.

If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi