1

This seems really simple but I've looked all over and I can't find any documentation for this.

I have the following json file:

//data.json
{
  "movie1": [
    {"name": "Inception"}
]}

and I just want to print the value of name with Ruby.

json = File.read('data.json')
data = JSON.parse(json)
data['movie1']['name']

But I'm getting the error

"no implicit conversoin of String into Integer"

How can I print name?

1
  • Viewing the result in ruby, e.g. pp data would give you clues as to the issue. Commented Feb 10, 2017 at 23:37

1 Answer 1

4

"no implicit conversion of String into Integer" usually comes when you're trying to use an array as a hash. Array[] expects an index (integer). Your data is a hash inside an array inside a hash :

You need :

data['movie1'][0]['name']

or

data.dig('movie1', 0, 'name')
Sign up to request clarification or add additional context in comments.

Comments

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.