1

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'

6
  • Parse to Hash: take a look at the docs: ruby-doc.org/stdlib-2.6.5/libdoc/json/rdoc/JSON.html, if not clear please, post back. Commented Oct 31, 2019 at 9:02
  • I checked but I don't get it, do I necessarily need to parse it ? Commented Oct 31, 2019 at 9:11
  • If your question is "Is the documentation true?", then the answer is: Yes. Commented Oct 31, 2019 at 9:22
  • "...access to display the value 'Alexis'" suggests you know 'Alexis' is the value of some key of some nested hash of some nested array. If so, puts 'Alexis' is all you need. If you don't know if there is such a value do you wish to return true or false, depending on whether if h[:testimonials][i][:authors] == 'Alexis'" for some i, implying you are aware of the structure of the hash?... Commented Oct 31, 2019 at 20:36
  • ...Or perhaps rather than returning true or false you wish to return g ={ "office": "Consultant", "authors": "Alexis", so that, for example, you could compute g[:office] #=> "Consultant", that corresponds with :authors having a value "Alexis". This is an example of trying to state a question unambiguously in terms of an example. Please edit to clarify. Commented Oct 31, 2019 at 20:40

1 Answer 1

1

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

4 Comments

Or hash.dig('testimonials', 4, 'authors') which will also take care of nil errors due to missing elements.
Yep, what @Stefan said. (but I had a feeling we were starting from a fairly remedial place and wanted to avoid the more clever methods, plus the ruby-on-rails-3 tag makes me assume we're probably not using ~> Ruby 2.3)
Good catch, I didn't see the ruby-on-rails-3 tag
Thanks for your time and your explanation ! I have an error 785: unexpected token at '{: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"}]}' while I parse it, do you where it comes from ? (I do a .to_s on my json file because I can't add string manually)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.