1

I'm getting a JSON response as below

Enroll Status -->{"data":[{"Id":"a082w000000YUH5AAO","EnrolStatus__c":"Submitted"}]}

How to read the object value in the response.( Always this will be a single object in an array);

@track enrollStatus;
 @wire(getEnrollmentStatus, {EnrollmentId: "$recordId" })
getenrollStatus(enrollstatus){
console.log('Enroll Status  -->' + JSON.stringify(enrollstatus))
 }



 public static List<Enrollment__c> getEnrollmentStatus(Id EnrollmentId) {          
    system.debug('ENROL ' + EnrollmentId);              
    return   [select  Id, EnrolStatus__c from Enrollment__c WHERE Id=:EnrollmentId];                
    }
3
  • Just directly e.g. let name = enrollstatus.Name; but note that names are case sensitive.
    – Keith C
    Commented Mar 17, 2020 at 11:19
  • @KeithC getenrollStatus(enrollstatus) { console.log("Enroll Status -->" + JSON.stringify(enrollstatus)); let enrollStatusvalue = enrollstatus.EnrolStatus__c; console.log('enrollStatus -->' + enrollStatusvalue); } Enroll Status -->{"data":[{"Id":"a082w000000YUH5AAO","EnrolStatus__c":"Submitted"}]} enrollStatus -->undefined
    – SK2889
    Commented Mar 17, 2020 at 12:20
  • @KeithC , The above one is not working, Please help on this.
    – SK2889
    Commented Mar 17, 2020 at 17:24

3 Answers 3

2

Generally speaking, the data you've returned is already an object/array. The following should work:

console.log(enrollmentStatus.data[0].Id);

Unless you're using a plain string in the Apex return type, it will already be an object you can use directly.

3
  • Apex return type is list <object> public static List<Enrollment__c>
    – SK2889
    Commented Mar 17, 2020 at 14:54
  • I'm getting Cannot read property 'EnrolStatus__c' of undefined
    – SK2889
    Commented Mar 17, 2020 at 17:27
  • var enrollstatusObj = JSON.parse(JSON.stringify(enrollstatus)); console.log("enrollstatusObj -->" + enrollstatusObj[0].EnrolStatus__c);
    – SK2889
    Commented Mar 17, 2020 at 17:27
1

You can parse it as below. As you are sure, the result always will have a single value in the array inside response, You can get the JavaScript array elements using index.

this.parsedValue = JSON.parse(enrollstatus);
console.log('this.parsedValue ' + this.parsedValue.data[0]);
console.log('this.parsedValue ' + this.parsedValue.data[0].Id);
console.log('this.parsedValue ' + this.parsedValue.data[0].EnrolStatus__c);

enter image description here

Parse the data with JSON.parse(), and the data becomes a JavaScript object.

4
  • Why wouldn't enrollstatus already be a JavaScript object? Otherwise why JSON.stringify it?
    – Keith C
    Commented Mar 17, 2020 at 11:57
  • That's a question we should ask from @user3214361 to confirm it. Commented Mar 17, 2020 at 12:02
  • @sanketkumar Yes the result will be always single value in the array,
    – SK2889
    Commented Mar 17, 2020 at 12:17
  • @sanketkumar I'm getting following error Cannot read property 'EnrolStatus__c' of undefined Please help on this. var enrollstatusObj = JSON.parse(JSON.stringify(enrollstatus)); console.log("enrollstatusObj -->" + enrollstatusObj[0].EnrolStatus__c);
    – SK2889
    Commented Mar 17, 2020 at 17:25
0

Use JSON.parse().

var enrollstatusObj = JSON.parse(enrollstatus);
var data = enrollstatusObj.data;
var id;
var enrollStatus;
if (data.length > 0) {
  id = data[o].Id;
  enrollStatus = data[0].EnrolStatus__c;
}
2
  • I'm getting following error Cannot read property 'EnrolStatus__c' of undefined Please help on this. var enrollstatusObj = JSON.parse(JSON.stringify(enrollstatus)); console.log("enrollstatusObj -->" + enrollstatusObj[0].EnrolStatus__c);
    – SK2889
    Commented Mar 17, 2020 at 17:26
  • @user3214361 There is no property EnrolStatus__c on enrollstatusObj[0]. You need to access the property using var data = enrollstatusObj.data; // access property using data[o]. EnrolStatus__c Please check my answer again. You'll have work on JavaScript mate. Commented Mar 18, 2020 at 10:07

You must log in to answer this question.

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