Anoop
Anoop

Reputation: 61

Append new values to an existing JSON array in ruby

I am new to ruby. I have an existing JSON file in the below format.

{
  "ASRtest": {
    "ASRHDR": "This is asr HDR",
    "ASRTestType": "DevTest",
    "Scenario": [
      {
        "ScenarioNumber": 1,
        "ScenarioName": "HTTP Validation",
        "ScenarioDescription": "Validate if the API alows access over HTTP",
        "ScExecutionStatus": "Execution Complete",
        "ScenarioStatus": "In-Complete",
        "ScenarioSeverity": false,
        "TestCase": [
          {
            "TestCaseNumber": 1,
            "TestCaseName": "HTTP Validation - using POST method ",
            "TcExecutionStatus": "Execution Error",
            "TcStatus": "NA",
            "TcSeverity": "NA"
          }
        ]
      }
    ]
  }
}

I am reading this file in my ruby program and want to another scenario to this file Like

{
  "ASRtest": {
    "ASRHDR": "This is asr HDR",
    "ASRTestType": "DevTest",
    "Scenario": [
      {
        "ScenarioNumber": 1,
        "ScenarioName": "HTTP Validation",
        "ScenarioDescription": "Validate if the API alows access over HTTP",
        "ScExecutionStatus": "Execution Complete",
        "ScenarioStatus": "In-Complete",
        "ScenarioSeverity": false,
        "TestCase": [
          {
            "TestCaseNumber": 1,
            "TestCaseName": "HTTP Validation - using POST method ",
            "TcExecutionStatus": "Execution Error",
            "TcStatus": "NA",
            "TcSeverity": "NA"
          }
        ]
      },
      {
        "ScenarioNumber": 2,
        "ScenarioName": "SC2",
        "ScenarioDescription": "Desc",
        "ScExecutionStatus": "Execution Complete",
        "ScenarioStatus": "In-Complete",
        "ScenarioSeverity": false,
        "TestCase": [
          {
            "TestCaseNumber": 1,
            "TestCaseName": "Some Name ",
            "TcExecutionStatus": "Execution Error",
            "TcStatus": "NA",
            "TcSeverity": "NA"
          }
        ]
      }
    ]
  }
}

I have read the file using the below code

@template_file = JSON.parse(File.read('SummaryTemplate.json'))
@ASR_Test = @template_file['ASRtest']
@ASR_Test
@scenario = @ASR_Test['Scenario']

when I try the below code

@scenario[1]['ScenarioNumber'] = 2

it gives me an error undefined method `[]=' for nil:NilClass (NoMethodError) the variable @scenario only has 1 occurrence and it does not allow me to add a second occurrence.

Could someone please help me with this issue.

Upvotes: 3

Views: 1233

Answers (2)

Lukas Baliak
Lukas Baliak

Reputation: 2869

In @scenario you have Array object, so if you wanna to add new hash inside this Array, just use Array#<< method like this.

Because the Array is object, you can add data inside this object.

new_scenario = {
  "ScenarioNumber" => 2,
  "ScenarioName" => "SC2",
  "ScenarioDescription" => "Desc",
  "ScExecutionStatus" => "Execution Complete",
  "ScenarioStatus" => "In-Complete",
  "ScenarioSeverity" => false,
  "TestCase" => [
    {
      "TestCaseNumber" => 1,
      "TestCaseName" => "Some Name ",
      "TcExecutionStatus" => "Execution Error",
      "TcStatus" => "NA",
      "TcSeverity" => "NA"
    }
  ]
}

@scenario << new_scenario

@template_file now include new_scenario

Upvotes: 0

Surya
Surya

Reputation: 16002

That's because from @scenario = @ASR_Test['Scenario'], @scenario will have:

[
  {
    "ScenarioNumber": 1,
    "ScenarioName": "HTTP Validation",
    "ScenarioDescription": "Validate if the API alows access over HTTP",
    "ScExecutionStatus": "Execution Complete",
    "ScenarioStatus": "In-Complete",
    "ScenarioSeverity": false,
    "TestCase": [
      {
        "TestCaseNumber": 1,
        "TestCaseName": "HTTP Validation - using POST method ",
        "TcExecutionStatus": "Execution Error",
        "TcStatus": "NA",
        "TcSeverity": "NA"
      }
    ]
  }
]

and saying @scenario[1] will produce: nil

Which means, calling @scenario[1]['ScenarioNumber'] will raise exception saying:

`[]=' for nil:NilClass (NoMethodError) the variable @scenario

To solve it, you will have to add a Hash object on given index:

@scenario[1] = {}

then your above code would work:

@scenario[1]['ScenarioNumber'] = 2

Upvotes: 1

Related Questions