Skip to content
Advertisement

Why am i getting ‘undefined’ from this JSON object? [closed]

var action=component.get("c.callCostCatalog");
        
action.setParams({ wrapperStructure:JSON.stringify(component.get("v.listStructurePV")),
              consumoTotal:component.get("v.consumTotal"), 
              Rate:'Vacia',
              orderItemID: component.get("v.recordId"),
              PMPInicial:component.get("v.precioInicial")})

action.setCallback(this,function(response){
//var listWrapper = JSON.parse(response.getReturnValue()); -> log shows [object object]
var listWrapper = JSON.parse(JSON.stringify(response.getReturnValue()));
//listWrapper.usedBand returns undefined
})

callCostCatalog is an Apex method which returns the string:

{"usedBand":0.0,"PMPObjetivo":0.0,"PMPNegotiated":0.028533,"PMPInit":0.028533,"negotiationBands":null,"Negotiation":0.0,"negBandCI":null,"minBandSD":null,"minBandRZ":null,"minBandRT":null,"minBandD":null,"minBand":null,"maxBandSD":null,"maxBandRZ":null,"maxBandRT":null,"maxBandD":null,"maxBand":null,"lNegotiatedPrices":[80.97],"lInitPrices":[0.028533],"lImplicitPrice":[],"lConsums":[]}

Advertisement

Answer

Your JSON.parse is right, and your console.log is right, and your JSON is valid.

However, you have erroneously called JSON.stringify, which is like the reverse of parse, taking a JavaScript object and producing a string of JSON. You don’t want that; you already have a string of JSON. Simply remove it.

Advertisement