简体   繁体   English

Nodejs无法提取JSON值

[英]Nodejs unable to extract JSON values

Variable SNS is a JSON object.变量 SNS 是一个 JSON 对象。 And I want to extract values from it and store it inside another variable.我想从中提取值并将其存储在另一个变量中。

  `const sns = event.Records[0].Sns.Message;`

I want to extract Trigger.Namespace and Trigger.Dimensions.value and Trigger.MetricName from the SNS JSON variable.我想从SNS JSON 变量中提取Trigger.NamespaceTrigger.Dimensions.valueTrigger.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)  

I get the following error:我收到以下错误:

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):

在此处输入图片说明

You should parse the JSON into an object like this:您应该将 JSON 解析为这样的对象:

const snsObj = JSON.parse(sns);

and then try to access the property:然后尝试访问该属性:

snsObj.Trigger.Namespace

There was no problem when testing with your console.log(sns) value.使用您的console.log(sns)值进行测试时没有问题。

Can I see the event data?我可以查看事件数据吗?

Based on your sns object log, there is nothing wrong :根据您的 sns 对象日志,没有任何问题:

 const sns = { "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": "" } } console.log(sns.Trigger.Namespace);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM