Parsing JSON

Need some help in parsing a string returned from a WS call. I'm getting back the following string: { ProyectoBE: [ { IdProject: '479', ProjectName: 'MacKay River 3D 2020', IdStatus: '1' }, { IdProject: '478', ProjectName: 'Pad 109 2020', IdStatus: '1' }, { IdProject: '477', ProjectName: 'Firebag 3D 4D 2020', IdStatus: '1' }, { IdProject: '480', ProjectName: 'Engina Mobilization', IdStatus: '1' }, { IdProject: '475',...

I'm calling this to set the string: let value = await client.Projects.ProjectsSoap.GetList({IdStatus: 1}) and get the results above.

I get an error when calling var obj = JSON.parse(value); [TASK:ERROR] SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse () at TaskContext.parse (/var/task/app/cloudcode/GetProjects/index.js:50:20) at process._tickCallback (internal/process/next_tick.js:68:7)

Any help would be appreciated. Thanks.

@MikeTiller What happens when you try console.log(value)? The error indicates that the value you are trying to parse is not valid JSON. There is also the possibility that value has already been parsed i.e. you are trying to parse it twice.

@MichaelBarnes I get this:

@MichaelBarnes 09:01:39.698 [TASK:INFO] { result: { GetListResult: { ProyectoBE: [Array] } }, envelope: ‘<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap=“” xmlns:xsi=“” xmlns:xsd=“”>soap:Body479MacKay River 3D 20201</soap:Body></soap:Envelope>’, soapHeader: undefined }

@MikeTiller It seems its already parsed. Could you try removing the JSON.parse?

@MichaelBarnes I tried just running through the array:for (let i in sanitizedResult) { console.log(sanitizedResult[i].ProyectoBE.ProjectName) }

@MichaelBarnes and get this: 09:18:05.898 [TASK:ERROR] TypeError: Cannot read property ‘ProjectName’ of undefined at TaskContext.ProjectName (/var/task/app/cloudcode/GetProjects/index.js:55:51) at process._tickCallback (internal/process/next_tick.js:68:7)

@MikeTiller without seeing the code, perhaps you need to try this to loop through the array for(let i of sanitizedResult.result.GetListResult.ProyectoBE) { console.log(i) } The error indicates that ProjectName is not a child of ProyectoBE i.e. undefined

As was alluded to previously, the data was already parsed and in object format and you can therefore just access the data directly using normal object notation.

The parsing error you received was as a result of trying to parse data that was not a valid JSON string.

A cursory glance at the logs for your CC task leads me to believe the issue has been resolved.