3

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.

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Surya sorry the above solution works only partially. It fixes the issue with scenario array but it does not solve the issue with the testcase array
0

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

3 Comments

Hi Lukas I had already tried to use this approach and it did not work. It just replaces the current object in scenario with the new one. The answer posted by Surya worked.
Anyway thanks for your response. Appreciate your time.
Hi Lukas I tried your solution again and it worked. The only tweak that I had to do was the long reference of the TestCase variable to set them up dynamically. new_scenario['TestCase'][0]['TestCaseNumber'] = 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.