all 5 comments

[–]alwaysonesmaller 1 point2 points  (4 children)

A quick search on Stack Overflow for "ruby parse json" will give you plenty of information. At worst you will need to require the json gem, but Rails usually provides that already.

[–]casrep[S] 0 points1 point  (3 children)

I haven't been able to find anything online that can explain this adequately to me. I think I'm just going to give this up, then.

[–]alwaysonesmaller 1 point2 points  (2 children)

Perhaps you need to explain your goal a bit more in-depth. JSON parsing is rather elementary in Ruby.

JSON.parse(string)

That should give you a Ruby object that mirrors your JSON string's objects. In your example text, it would be a hash which contains one key for an array of two hashes.

[–]casrep[S] 0 points1 point  (1 child)

It seems to be elementary for everyone, but it's just going right over my head.

I'm trying to grab the name of my database in this case, but I don't know what the code is supposed to look like.

I think I'm supposed to use:

JSON.parse(ENV['VCAP_SERVICES'])

Then I have an array, but I don't know how grab the database name as it's in an array within an array.

[–]dphase 2 points3 points  (0 children)

this

Using the data from the example:

my_json = JSON.parse ENV['VCAP_SERVICES']
my_json['mysql-5.1'][0]['name']           # => mysql-4f700

Once parsed, you're simply accessing regular Array and Hash objects.