-1

I have this complex hash called data in a JSON file, just below. I am interested in the field "videoId"

so i ran this code :

puts data["videoId"] 

But i get no results. What would be the best way to get all the videosid in ruby ? and why code is not working ?

{
    "etag": "\"DuHzAJ-eQIiCIp7p4ldoVcVAOeY/2rushbFMdJCgH0PsUecXvdIjrIA\"",
    "items": [
        {
            "etag": "\"DuHzAJ-eQIiCIp7p4ldoVcVAOeY/SGIvedEKHZeJcInXZg-T4Lde7gE\"",
            "id": "TExreW5lbzZYQXYxbm8xakgzM0syQm1nLnhTWTc1cUpfa0lr",
            "kind": "youtube#playlistItem",
            "snippet": {

                "publishedAt": "2018-05-21T16:00:00.000+00:00",
                "resourceId": {
                    "kind": "youtube#video",
                    "videoId": "xSY75qJ_kIk"
                },

                "title": "The Key (Wax Trax Mix) - Space Bunny | Surreal [1998]"
            }
        },
        {
            "etag": "\"DuHzAJ-eQIiCIp7p4ldoVcVAOeY/Z7hQMBr8AzIyI7n2LPBnd7AUwjs\"",
            "id": "TExreW5lbzZYQXYxbm8xakgzM0syQm1nLmVISWd4c2EzckVz",
            "kind": "youtube#playlistItem",
            "snippet": {

                "publishedAt": "2018-05-18T08:00:00.000+00:00",
                "resourceId": {
                    "kind": "youtube#video",
                    "videoId": "eHIgxsa3rEs"

                    }
                },
                "title": "Coin coin Pata'tribe mix [Old School Tekno Tribe]"
            }
        },
        {
            "etag": "\"DuHzAJ-eQIiCIp7p4ldoVcVAOeY/T2xQFVjUHZYJ2FHGa4Yb82yMubc\"",
            "id": "TExreW5lbzZYQXYxbm8xakgzM0syQm1nLmgxMFlVSVVxb1lN",
            "kind": "youtube#playlistItem",
            "snippet": {

                "publishedAt": "2018-05-17T16:00:00.000+00:00",
                "resourceId": {
                    "kind": "youtube#video",
                    "videoId": "h10YUIUqoYM"
                },

                "title": "Lil Louis 1992.09.25 @ Hacienda"
            }
        },

2 Answers2

0

This is not an array, this is a hash. To get all videoIds, one might [Enumerable#map] items array to dig the values:

data["items"].map { |item| item["snippet"]["resourceId"]["videoId"] }

resulting in the array of videoIds.


Alternatively, one might use Iteraptor gem to just grep for the videoIds:

data.segar(/videoId/) { |*args| puts args.inspect }
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
0

A Hash is a dictionary-like collection of unique keys and their values. Also called associative arrays, they are similar to Arrays, but where an Array uses integers as its index, a Hash allows you to use any object type.

Of course, a value can be any object - string, method, nil, number, object. So, only after create, we can know that is saved in our hash. For this reason when trying to get all key:

data.keys # => ["etag", "items"]

There is no any nested key. So finding value by missing key return nil.

To select all videoId you must do something like this:

data["items"].map { |item| item["snippet"]["resourceId"]["videoId"] }.compact

Also, you can update Hash class as there:

class Hash
  def deep_find(key, object=self, found=nil)
    if object.respond_to?(:key?) && object.key?(key)
      return object[key]
    elsif object.is_a? Enumerable
      object.find { |*a| found = deep_find(key, a.last) }
      return found
    end
  end
end

And then run

data["items"].map { |item| item.deep_find('videoId') }

That method avoids the error when the json will have a variable structure.

Leo
  • 1,673
  • 1
  • 13
  • 15