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
d.tejdeep@nicomatic.ind.tejdeep@nicomatic.in 

check response value json is valid or not in salesforce lightning component controller ?

I want to check whether a response.getReturnValue() is valid JSON or not .if it is a valid JSON then it should parse otherwise it shows error  
if (state === "SUCCESS") {
                   console.log(response.getReturnValue());
                    var jsonData=JSON.parse(response.getReturnValue());
                    console.log(jsonData); 
                    if(!jsonData){
                    component.set("v.Servererror",'Null');
                    }else(jsonData['error']!=null){
                      component.set("v.Servererror",jsonData['error']);
                    }
             }

How to check whether it is a valid JSON or not
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Tejdeep 

Use try catch for this : 

if (state === "SUCCESS") {
console.log(response.getReturnValue());
try {
var responseData = response.getReturnValue() || {};
var jsonData=JSON.parse(responseData);
console.log(jsonData);
} catch (err) {
component.set("v.Servererror",err);
}
}

Cheers!!!