Nodejs 无法提取 JSON 值
Nodejs unable to extract JSON values
变量 SNS 是一个 JSON 对象。我想从中提取值并将其存储在另一个变量中。
`const sns = event.Records[0].Sns.Message;`
我想从 SNS
JSON 变量中提取 Trigger.Namespace
和 Trigger.Dimensions.value
以及 Trigger.MetricName
。
const sns_NameSpace = sns.Trigger.Namespace;
const sns_ApiId = sns.Trigger.Dimensions.value;
const sns_MetricName = sns.Trigger.MetricName;
console.log(sns_ApiId + '_' + sns_MetricName)
console.log(sns_NameSpace)
我收到以下错误:
ERROR Invoke Error
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'Namespace' of undefined",
"stack": [
"TypeError: Cannot read property 'Namespace' of undefined",
" at Runtime.exports.handler (/var/task/index.js:7:38)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
console.log(sns) returns the following:
{
"AlarmName": "ZabbixPy 5XX Error - HTTP",
"AlarmDescription": null,
"AWSAccountId": "123456789",
"NewStateValue": "ALARM",
"NewStateReason": "Threshold Crossed: 1 out of the last 1 datapoints [24.0 (01/11/21 11:42:00)] was greater than or equal to the threshold (1.0) (minimum 1 datapoint for OK -> ALARM transition).",
"StateChangeTime": "2021-11-01T11:43:35.422+0000",
"Region": "Asia Pacific (Mumbai)",
"AlarmArn": "arn:aws:cloudwatch:ap-south-1:1234567890:alarm:ZabbixPy 5XX Error - HTTP",
"OldStateValue": "INSUFFICIENT_DATA",
"Trigger": {
"MetricName": "5xx",
"Namespace": "AWS/ApiGateway",
"StatisticType": "Statistic",
"Statistic": "SUM",
"Unit": null,
"Dimensions": [
{
"value": "someid",
"name": "ApiId"
}
],
"Period": 60,
"EvaluationPeriods": 1,
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"Threshold": 1,
"TreatMissingData": "- TreatMissingData: missing",
"EvaluateLowSampleCountPercentile": ""
}
}
Event data when trying to filter out:
Event data without any filters - console.log(sns):
使用您的 console.log(sns)
值进行测试时没有问题。
我能看到活动数据吗?
您应该将 JSON 解析为这样的对象:
const snsObj = JSON.parse(sns);
然后尝试访问 属性:
snsObj.Trigger.Namespace
变量 SNS 是一个 JSON 对象。我想从中提取值并将其存储在另一个变量中。
`const sns = event.Records[0].Sns.Message;`
我想从 SNS
JSON 变量中提取 Trigger.Namespace
和 Trigger.Dimensions.value
以及 Trigger.MetricName
。
const sns_NameSpace = sns.Trigger.Namespace;
const sns_ApiId = sns.Trigger.Dimensions.value;
const sns_MetricName = sns.Trigger.MetricName;
console.log(sns_ApiId + '_' + sns_MetricName)
console.log(sns_NameSpace)
我收到以下错误:
ERROR Invoke Error
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'Namespace' of undefined",
"stack": [
"TypeError: Cannot read property 'Namespace' of undefined",
" at Runtime.exports.handler (/var/task/index.js:7:38)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
]
console.log(sns) returns the following:
{
"AlarmName": "ZabbixPy 5XX Error - HTTP",
"AlarmDescription": null,
"AWSAccountId": "123456789",
"NewStateValue": "ALARM",
"NewStateReason": "Threshold Crossed: 1 out of the last 1 datapoints [24.0 (01/11/21 11:42:00)] was greater than or equal to the threshold (1.0) (minimum 1 datapoint for OK -> ALARM transition).",
"StateChangeTime": "2021-11-01T11:43:35.422+0000",
"Region": "Asia Pacific (Mumbai)",
"AlarmArn": "arn:aws:cloudwatch:ap-south-1:1234567890:alarm:ZabbixPy 5XX Error - HTTP",
"OldStateValue": "INSUFFICIENT_DATA",
"Trigger": {
"MetricName": "5xx",
"Namespace": "AWS/ApiGateway",
"StatisticType": "Statistic",
"Statistic": "SUM",
"Unit": null,
"Dimensions": [
{
"value": "someid",
"name": "ApiId"
}
],
"Period": 60,
"EvaluationPeriods": 1,
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"Threshold": 1,
"TreatMissingData": "- TreatMissingData: missing",
"EvaluateLowSampleCountPercentile": ""
}
}
Event data when trying to filter out:
Event data without any filters - console.log(sns):
使用您的 console.log(sns)
值进行测试时没有问题。
我能看到活动数据吗?
您应该将 JSON 解析为这样的对象:
const snsObj = JSON.parse(sns);
然后尝试访问 属性:
snsObj.Trigger.Namespace