0

I am receiving JOSN string in the following format

{"data":{"meta":{"resultCode":null,"message":null},"data":[{"code":"$RFG","Token":288}]}}

I am trying to fetch value of code and Token from the JSON string and I want to use it in LWC html. I tried in may ways but I am not able to fetch the value as expected. Somewhere I am missing some part. Please let me know where I am going wrong.

import { LightningElement, api, wire, track } from 'lwc';
import getToken from '@salesforce/apex/ProcessToken.getToken';
export default class tokenProcess extends LightningElement {
    @track ww;
    @api recordId;
    @wire (getToken, {Id : '$recordId'})
    entitlement(tokenDetails, error){
     if(tokenDetails)
     {
      
        this.ww = tokenDetails.data;
        console.log('DDDDD@@@@@@@'+ this.ww);
       this.ww.data.forEach(meta => {
        console.log(meta.code, meta.Token);
    });
    
  } 
     else if(error)
     {
        
    }
}
}

In Console:

DDDDD@@@@@@@ : {"meta":{"resultCode":null,"message":null},"data":[{"code":"$RFG","Token":288}]}

After this I am not able to fetch it.

3
  • 2
    Do you receive this as a JavaScript object or as a JSON string? If the latter you turn it into an object using JSON.parse.
    – Phil W
    May 7, 2021 at 6:46
  • I am returning as JSON string from Apex method but if you see my console.log('DDDDD@@@@@@@'+ this.ww); I am able to fetch it without JSON.parse(); that's where I am confused
    – Mangoski
    May 7, 2021 at 7:13
  • 1
    The console log only shows that there is JSON text (indeed, I suspect the output would be DDDDD@@@@@@[object Object] if this were a JavaScript object already). To turn it into a JavaScript object you can navigate you must use JSON.parse. The question would be - why return a JSON string from your Apex? Better to return an object directly to slightly reduce the size of the payload and to avoid having to parse the response. You can use custom Apex class instances or Map<String, Object> to achieve the same result (though the former is better; it gives well known structure and names).
    – Phil W
    May 7, 2021 at 7:42

1 Answer 1

0

first, it would be important to know what type your getToken method is returning from apex. (String, Map<String,Object>, List, etc, etc...)

this will allow you to understand what the client receives, and how to handle the response.

based on the above, you might, or might not need to parse the string in order to convert it to an object. if you are expecting multiple code/token objects in your data array and need to fetch them all separately, you can do it in a forEach loop as you seem to be already doing, if you are only expecting 1, then, instead you could use a Array.prototype.find() to get the objects and assign to variables accordingly.

2
  • It's returning JSON String. It's a common method which is called by many other process so I can't change the return type. That's the challenge I am facing eevnthough If I use JSON.parse I am not able to achieve it
    – Mangoski
    May 10, 2021 at 13:43
  • you can attempt to "Stringify" before parsing
    – glls
    May 10, 2021 at 15:15

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.