[英]How to retrieve specific value from list of Json Object in NodeJS
I'm trying the below code to retrieve the executionArn but I'm getting this error我正在尝试使用以下代码来检索executionArn,但出现此错误
Error [SyntaxError]: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
How to get executionArn or stateMachineArn from each record?如何从每条记录中获取executionArn或stateMachineArn ? Any help would be much appreciated.
任何帮助将非常感激。
console.log(data) - Output console.log(data) - 输出
{
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);
});
});
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)
})
executions.map(e=>{
// access it here
let a = e.executionArn;
});
The provided output (data) is not a valid JSON object,提供的输出(数据)不是有效的 JSON 对象,
{...
startDate: 2021-06-17T13:43:39.817Z,
stopDate: 2021-06-17T13:43:53.667Z
}
For valid JSON, it should look like对于有效的 JSON,它应该看起来像
{...
"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如果数据来自服务器(API),则在返回之前将日期字符串化,并且可以通过以下方式找到值
data.executions.map(execution=>{
let arnValue = execution.executionArn;
console.log(arnValue);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.