Damien Compère
Damien Compère

Reputation: 269

How to access key, value in json / ruby

I have a json file like this, and I try to display the different value in my html:

{
    "testimonials": [
        {
            "office": "Test",
            "authors": "Benjamin",
        },
        {
            "office": "consultant",
            "authors": "Maxime ",
        },
        {
            "office": "DAF",
            "authors": "Alexandre",

        },
        {
            "office": "CEO",
            "authors": "Raphaël",
          },
        {
            "office": "Consultant",
            "authors": "Alexis",
        },
        {
            "office": "CEO,",
            "authors": "Sylvain",
        }
    ]
}

Could someone help me, for example, to access to display the value 'Alexis'

Upvotes: 1

Views: 3914

Answers (1)

anothermh
anothermh

Reputation: 10564

This is not valid JSON due to the trailing commas within your hashes. If you fix the commas, minify the JSON to make it easier to work with, and save it as a string, then you can begin to work with it in Ruby:

json = '{"testimonials":[{"office":"Test","authors":"Benjamin"},{"office":"consultant","authors":"Maxime "},{"office":"DAF","authors":"Alexandre"},{"office":"CEO","authors":"Raphaël"},{"office":"Consultant","authors":"Alexis"},{"office":"CEO,","authors":"Sylvain"}]}'

Now parse it into a real Ruby object:

hash = JSON.parse(json)
=> {
    "testimonials" => [
        [0] {
             "office" => "Test",
            "authors" => "Benjamin"
        },
        [1] {
             "office" => "consultant",
            "authors" => "Maxime "
        },
        [2] {
             "office" => "DAF",
            "authors" => "Alexandre"
        },
        [3] {
             "office" => "CEO",
            "authors" => "Raphaël"
        },
        [4] {
             "office" => "Consultant",
            "authors" => "Alexis"
        },
        [5] {
             "office" => "CEO,",
            "authors" => "Sylvain"
        }
    ]
}

This is a hash that has an array of hashes inside it. You should access it using the standard methods for Hash and Array.

Start by getting the value of the only key in the hash, which is an array:

array = hash['testimonials']
=> [
    [0] {
         "office" => "Test",
        "authors" => "Benjamin"
    },
    [1] {
         "office" => "consultant",
        "authors" => "Maxime "
    },
    [2] {
         "office" => "DAF",
        "authors" => "Alexandre"
    },
    [3] {
         "office" => "CEO",
        "authors" => "Raphaël"
    },
    [4] {
         "office" => "Consultant",
        "authors" => "Alexis"
    },
    [5] {
         "office" => "CEO,",
        "authors" => "Sylvain"
    }
]

You indicated you wanted to fetch a value from index 4:

sub_hash = array[4]
=> {
     "office" => "Consultant",
    "authors" => "Alexis"
}

And that you wanted to return the string Alexis:

string = sub_hash['authors']
=> "Alexis"

Or put it all together in one line:

string = hash['testimonials'][4]['authors']
=> "Alexis"

Or one even shorter line:

JSON.parse(json)['testimonials'][4]['authors']
=> "Alexis"

Upvotes: 1

Related Questions