Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replace NULL with NIL when using JSON or Hash

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I resolve this?

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,

>Solution :

Me again, i’ve been trying some things. Tell me if this resolves your problem.

require 'json'

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading