1

I am getting below json from backend.

{ drug:
  {

    "id": "580f323ee4b06ffee69041a1",
    "direction": [
        {
            "direction": "test",
            "discarded": false
        }
    ]
  }
}

I dont want direction as array. I want it as object so I wrote method drug_format to parse json

My ruby on rails code for parsing is as follows :

def drug_format drug
{
  id: drug[:id],
  direction: drug[:direction][0],
}
end

Now when I am trying to run my code I am getting following error.

NoMethodError (undefined method `[]' for nil:NilClass):
    app/controllers/drugs_controller.rb:280:in `drug_format'
    app/controllers/drugs_controller.rb:15:in `block in index'
    app/controllers/drugs_controller.rb:14:in `each'
    app/controllers/drugs_controller.rb:14:in `index'

What can be the issue?

7
  • How do you call drug_format? Commented Oct 26, 2016 at 10:28
  • drug_format(drug) Commented Oct 26, 2016 at 10:58
  • What is drug and what does drug[:id] or drug[:direction] return? Commented Oct 26, 2016 at 10:59
  • drug[:id] returns string and drug[:direction] returns array of object. I want to access first object of array. Commented Oct 26, 2016 at 11:08
  • If drug is a hash and drug[:direction] returns an array, then neither drug[:id] nor drug[:direction][0] would raise that error. Commented Oct 26, 2016 at 11:19

1 Answer 1

2

the json keys are strings and not symbols, so you need something like:

def drug_format drug
  {
    id: drug['id'],
    direction: drug['direction'][0]
  }
end

or you can use with_indifferent_access

console output:

params = {
 'drug' => {
    "id" => "580f323ee4b06ffee69041a1",
    "direction" => [
        {
            "direction" => "test",
            "discarded" => false
        }
    ]
  }
}
=> {"drug"=>{"id"=>"580f323ee4b06ffee69041a1", "direction"=>[{"direction"=>"test", "discarded"=>false}]}}

params[:drug]
=> nil

params['drug']
=> {"id"=>"580f323ee4b06ffee69041a1", "direction"=>[{"direction"=>"test", "discarded"=>false}]}

def drug_format(drug)
  {
    id: drug['id'],
    direction: drug['direction'][0]
  }
end
=> :drug_format

drug_format(params[:drug])
=> NoMethodError: undefined method `[]' for nil:NilClass

drug_format(params['drug'])
=> {:id=>"580f323ee4b06ffee69041a1", :direction=>{"direction"=>"test", "discarded"=>false}}
Sign up to request clarification or add additional context in comments.

8 Comments

it might an issue accessing the drug param... please note my edited answer
GEnerated JSON is not correct. Take away this comma in direction: drug[:direction][0],, because there is no reason for it to be there. A comma says there will be another key after that element.
@EddeAlmeida that doesn't matter in ruby
@EddeAlmeida just checked your profile, correct if I said something wrong.. You shoud have digged ruby a lot ;)
Yes I did and the , is handeled so {a: 'abc', } dosn't throw any errors
|

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.