I have the following JSON and I'm trying to replace null
with nil
because ruby can't compile it.
I get the following error undefined local variable or method "null" for main:Object (NameError)
.
AlarmDescription and Unit is null
.
How can I resolve this? I tried replace
and gsub
but couldn't replace null
with nil
.
jsondata = {
AlarmName: "Zabbix PY 5XX - By Stage QA",
AlarmDescription: null,
AWSAccountId: "123456",
NewStateValue: "ALARM",
NewStateReason:
"Threshold Crossed: 1 out of the last 1 datapoints [1.0 (06/11/21 12:16:00)] was greater than or equal to the threshold (1.0) (minimum 1 datapoint for OK -> ALARM transition).",
StateChangeTime: "2021-11-06T12:17:43.811+0000",
Region: "Asia Pacific (Mumbai)",
AlarmArn: "arn:aws:cloudwatch:ap-south-1:123456:alarm:Zabbix PY 5XX - By Stage QA",
OldStateValue: "INSUFFICIENT_DATA",
Trigger: {
MetricName: "5XXError",
Namespace: "AWS/ApiGateway",
StatisticType: "Statistic",
Statistic: "MINIMUM",
Unit: null,
Dimensions: [
{ value: "zabbixPy-API", name: "ApiName" },
{ value: "qa", name: "Stage" },
],
Period: 60,
EvaluationPeriods: 1,
ComparisonOperator: "GreaterThanOrEqualToThreshold",
Threshold: 1,
TreatMissingData: "- TreatMissingData: missing",
EvaluateLowSampleCountPercentile: "",
},
}
puts jsondata
Error:
Output:
HelloWorld.rb:6:in `<main>': undefined local variable or method `null' for main:Object (NameError)
AlarmDescription: null,
Me again, i've been trying some things. Tell me if this resolves your problem.
texte = %q(
{
AlarmName: "Zabbix PY 5XX - By Stage QA",
AlarmDescription: null,
AWSAccountId: "123456",
NewStateValue: "ALARM",
NewStateReason:
"Threshold Crossed: 1 out of the last 1 datapoints [1.0 (06/11/21 12:16:00)] was greater than or equal to the threshold (1.0) (minimum 1 datapoint for OK -> ALARM transition).",
StateChangeTime: "2021-11-06T12:17:43.811+0000",
Region: "Asia Pacific (Mumbai)",
AlarmArn: "arn:aws:cloudwatch:ap-south-1:123456:alarm:Zabbix PY 5XX - By Stage QA",
OldStateValue: "INSUFFICIENT_DATA",
Trigger: {
MetricName: "5XXError",
Namespace: "AWS/ApiGateway",
StatisticType: "Statistic",
Statistic: "MINIMUM",
Unit: null,
Dimensions: [
{ value: "zabbixPy-API", name: "ApiName" },
{ value: "qa", name: "Stage" }
],
Period: 60,
EvaluationPeriods: 1,
ComparisonOperator: "GreaterThanOrEqualToThreshold",
Threshold: 1,
TreatMissingData: "- TreatMissingData: missing",
EvaluateLowSampleCountPercentile: ""
}
}
)
texte = texte.gsub('null', 'nil')
ruby_hash = eval(texte)
puts ruby_hash.inspect
puts "----"
puts ruby_hash[:Trigger][:EvaluationPeriods]