Reputation:
I have a JSON response from an API, which I have converted into a hash, I would like to extract the value for the key title, director, overview and the arrays for availability and trailer. I have tried extract! and slice both seem to have difficulty find the key value combination.
{"availability"=>{}, "created"=>"2013-01-08T08:02:04.349441+00:00", "data"=>{"adult"=>false, "alternative_titles"=>[], "backdrop_path"=>"https://image.tmdb.org/t/p/w1280/eN5XFV9fyLfoTsBDKssrS4fu515.jpg", "belongs_to_collection"=>{"backdrop_path"=>"/zvZBNNDWd5LcsIBpDhJyCB2MDT7.jpg", "id"=>8650, "name"=>"Transformers Collection", "poster_path"=>"/giURBCO3r4LVbhywEjIuYz92fEv.jpg"}, "budget"=>0, "cast"=>[{"character"=>"Trypticon (voice)", "id"=>15831, "name"=>"Frank Welker", "profile_path"=>"https://image.tmdb.org/t/p/w185/a3QPvpgqKMGViS2M9mGcRy7xDZ.jpg"}, {"character"=>"Scourge (voice)", "id"=>15860, "name"=>"Miguel Ferrer", "profile_path"=>"https://image.tmdb.org/t/p/w185/fvRncqShcaV8R45IJGYZ7IAvrsO.jpg"}, {"character"=>"Powerglide (voice)", "id"=>27993, "name"=>"Jeffrey Combs", "profile_path"=>"https://image.tmdb.org/t/p/w185/eAwMDGN99JAjbkDY2KkH0e00Hct.jpg"}, {"character"=>"Thrust (voice)", "id"=>6576, "name"=>"Mark Rolston", "profile_path"=>"https://image.tmdb.org/t/p/w185/bsh3cqDNwVvux4NdaY1Bj4S7mNS.jpg"}, {"character"=>nil, "id"=>13240, "name"=>"Mark Wahlberg", "profile_path"=>"https://image.tmdb.org/t/p/w185/eGHgl0sRg0ecKuAiHKehPUnY1lL.jpg"}, {"character"=>"", "id"=>61186, "name"=>"Nicola Peltz", "profile_path"=>"https://image.tmdb.org/t/p/w185/ywS7lEeN6kbZCOUH3U2Rxiw1DFc.jpg"}, {"character"=>"", "id"=>2283, "name"=>"Stanley Tucci", "profile_path"=>"https://image.tmdb.org/t/p/w185/omGlTJF2IW5r3L3c5y0qkCt3hFr.jpg"}], "crew"=>[{"department"=>"Directing", "id"=>865, "job"=>"Director", "name"=>"Michael Bay", "profile_path"=>"https://image.tmdb.org/t/p/w185/cJNLMOqIkGZDdwkwSq1YpgiMp9P.jpg"}], "directors"=>[{"department"=>"Directing", "id"=>865, "job"=>"Director", "name"=>"Michael Bay", "profile_path"=>"https://image.tmdb.org/t/p/w185/cJNLMOqIkGZDdwkwSq1YpgiMp9P.jpg"}], "genres"=>[{"id"=>878, "name"=>"Science Fiction"}], "homepage"=>"", "id"=>91314, "imdb_id"=>"tt2109248", "original_title"=>"Transformers: Age of Extinction", "overview"=>"Fourth part of the Transformers series, which starts a new main characters changing history.", "popularity"=>4.98715854566441, "poster_path"=>"https://image.tmdb.org/t/p/w342/k8utnXyWgMXKz6QVTOfwMsNrAJk.jpg", "production_companies"=>[], "production_countries"=>[{"iso_3166_1"=>"US", "name"=>"United States of America"}], "release_date"=>"2014-06-27", "releases"=>{"AR"=>{"rating"=>"", "release_date"=>"2014-07-10"}, "AU"=>{"rating"=>"", "release_date"=>"2014-06-26"}, "BE"=>{"rating"=>"", "release_date"=>"2014-06-25"}, "BR"=>{"rating"=>"", "release_date"=>"2014-07-19"}, "CH"=>{"rating"=>"", "release_date"=>"2014-07-17"}, "CL"=>{"rating"=>"", "release_date"=>"2014-07-10"}, "DE"=>{"rating"=>"", "release_date"=>"2014-07-17"}, "DK"=>{"rating"=>"", "release_date"=>"2014-06-26"}, "ES"=>{"rating"=>"", "release_date"=>"2014-08-08"}, "FR"=>{"rating"=>"", "release_date"=>"2014-07-02"}, "GB"=>{"rating"=>"", "release_date"=>"2014-07-10"}, "HK"=>{"rating"=>"", "release_date"=>"2014-06-26"}, "HU"=>{"rating"=>"", "release_date"=>"2014-06-26"}, "IT"=>{"rating"=>"", "release_date"=>"2014-07-16"}, "LT"=>{"rating"=>"", "release_date"=>"2014-06-27"}, "MX"=>{"rating"=>"", "release_date"=>"2014-07-11"}, "NL"=>{"rating"=>"", "release_date"=>"2014-07-09"}, "NO"=>{"rating"=>"", "release_date"=>"2014-06-27"}, "PE"=>{"rating"=>"", "release_date"=>"2014-07-10"}, "PT"=>{"rating"=>"", "release_date"=>"2014-06-26"}, "RU"=>{"rating"=>"", "release_date"=>"2014-06-26"}, "SE"=>{"rating"=>"", "release_date"=>"2014-06-27"}, "SG"=>{"rating"=>"", "release_date"=>"2014-06-26"}, "TR"=>{"rating"=>"", "release_date"=>"2014-06-27"}, "US"=>{"rating"=>"", "release_date"=>"2014-06-27"}}, "revenue"=>0, "runtime"=>0, "spoken_languages"=>[{"iso_639_1"=>"en", "name"=>"English"}], "status"=>"Planned", "tagline"=>"", "title"=>"Transformers: Age of Extinction", "trailers"=>{"id"=>91314, "quicktime"=>[], "youtube"=>[]}, "vote_average"=>8.0, "vote_count"=>1}, "key"=>"p30RBh.transformers-4", "last_mod"=>"2014-01-19T21:52:32.021978+00:00", "last_refresh"=>"2014-01-19T21:52:32.012421+00:00", "lookup_key"=>"p30RBh.transformers-age-of-extinction", "matches"=>{"netflix"=>"catalog/titles/movies/70299855"}, "path"=>"p30RBh/transformers-age-of-extinction", "release_timestamp"=>1403827200, "salt"=>"p30RBh", "sort_keys"=>{"as_is"=>"Transformers: Age of Extinction\u001Fp30RBh", "ignore_case"=>"transformers: age of extinction\u001Fp30RBh", "ignore_case_normalized"=>"transformers: age of extinction\u001Fp30RBh", "no_article"=>"transformers: age of extinction\u001Fp30RBh", "no_article_normalized"=>"transformers: age of extinction\u001Fp30RBh", "no_article_symbols"=>"transformers age of extinction\u001Fp30RBh", "no_symbols"=>"transformers age of extinction\u001Fp30RBh"}, "source"=>{"id"=>91314, "service"=>"themoviedb"}, "version"=>15}
Upvotes: 0
Views: 311
Reputation: 30310
You don't need extract
or slice
, but to find the title in that JSON, you would simply use the []
function for Hash
like this:
hash["data"]["title"]
Meanwhile, hash["data"]["directors"]
gets you an array of objects:
[{"department"=>"Directing", "id"=>865, "job"=>"Director", "name"=>"Michael Bay", "profile_path"=>"https://image.tmdb.org/t/p/w185/cJNLMOqIkGZDdwkwSq1YpgiMp9P.jpg"}]
Either look at the Netflix API documentation, eyeball the keys, use a debugger, or call keys
on all your hashes starting from hash["data"]
in your console to figure out how to navigate down the tree to the data you want.
Upvotes: 1