0

I'm trying the below code to retrieve the executionArn but I'm getting this error

Error [SyntaxError]: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)

How to get executionArn or stateMachineArn from each record? Any help would be much appreciated.

console.log(data) - Output

{
  executions: [
    {
      executionArn: 'arn:aws:states:us-east-2:12222:execution:test:dcb689bc',
      stateMachineArn: 'arn:aws:states:us-east-2:12222:stateMachine:test-sm',
      name: 'test-name',
      status: 'SUCCEEDED',
      startDate: 2021-06-17T13:43:39.817Z,
      stopDate: 2021-06-17T13:43:53.667Z
    },
    {
      executionArn: 'arn:aws:states:us-east-2:12222:execution:test:sd32dsdf',
      stateMachineArn: 'arn:aws:states:us-east-2:12222:stateMachine:test-sm',
      name: 'test-name1',
      status: 'SUCCEEDED',
      startDate: 2021-06-17T13:43:39.817Z,
      stopDate: 2021-06-17T13:43:53.667Z
    }
  ],
  nextToken: 'aadfdfdf'
}

Code:

  stepfunctions.listExecutions(params, function(err, data) {
    if (err) console.log(err, err.stack); 
    else     
    console.log(data)
    //console.log(data.executions[0].executionArn)
    data = JSON.parse(data);
    data.forEach(function(result) {
        var arnValue = result.executions.executionArn;
        console.log(arnValue);
    });

  });
5
  • What is the output of console.log(data)? Please be aware that the input is not valid JSON. Commented Jun 17, 2021 at 14:24
  • Updated my post - pls check this console.log(data) - Output Commented Jun 17, 2021 at 14:26
  • data looks like it's a JavaScript object, not JSON. There should be no need to use JSON.parse Commented Jun 17, 2021 at 14:27
  • If I don't use JSON.parse then its giving me a data.forEach is not a function :( Commented Jun 17, 2021 at 14:35
  • 1
    Probably because data is an object which doesn't have that function. Try using data.executions.forEach instead? Commented Jun 17, 2021 at 14:36

3 Answers 3

1

data is an object and executions inside it is an array, so try this

data.executions.foreach(function (execution) {
  console.log('executionArn', execution.executionArn)
  console.log('stateMachineArn', execution.stateMachineArn)
})
Sign up to request clarification or add additional context in comments.

Comments

0
executions.map(e=>{
   // access it here
   let a =  e.executionArn;
 });

Comments

0

The provided output (data) is not a valid JSON object,

{...
  startDate: 2021-06-17T13:43:39.817Z,
  stopDate: 2021-06-17T13:43:53.667Z
}

For valid JSON, it should look like

{...
  "startDate": "2021-06-17T13:43:39.817Z",
  "stopDate": "2021-06-17T13:43:53.667Z"
}

for it to be iterated correctly.

If the data is from the server (API), stringify the dates before returning them, and the values could be found by

  data.executions.map(execution=>{
      let arnValue = execution.executionArn;
      console.log(arnValue);
  })

2 Comments

The JSON example is not valid JSON. Also There's no such thing as a "JSON Object"
thanks @evolutionxbx. Its a common mistake we make as developers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.