0

I'll start out by saying that I am a pretty new web developer, so I apologize if this is overly basic... I just couldn't find it anywhere on Google. I'm receiving JSON data back from an API call to omdb, and I am unsure how to reference a line in the data Specifically, I am trying to reference the Rotten Tomatoes Value, and this needs to be repeatable for any movie I search. I started by storing the response in JSON and then working through each item I need:

var body = JSON.parse(body);
console.log("Title: " + body.Title);
console.log("Release Year: " + body.Year);
console.log("IMdB Rating: " + body.imdbRating);
console.log("Country: " + body.Country);
console.log("Language: " + body.Language);
console.log("Plot: " + body.Plot);
console.log("Actors: " + body.Actors);
console.log("Rotten Tomatoes Rating: " + body.Ratings.????RottenTomatoes???);

It's just the Rotten Tomatoes Value Line I can't figure out! Everything else works. To clarify, this is just a JSON referencing issue I cannot figure out.

{
  "Title": "Anastasia",
  "Year": "1997",
  "Rated": "G",
  "Released": "21 Nov 1997",
  "Runtime": "94 min",
  "Genre": "Animation, Adventure, Drama",
  "Director": "Don Bluth, Gary Goldman",
  "Writer": "Susan Gauthier (screenplay), Bruce Graham (screenplay), Bob Tzudiker (screenplay), Noni White (screenplay), Eric Tuchman (animation adaptation)",
  "Actors": "Meg Ryan, John Cusack, Kelsey Grammer, Christopher Lloyd",
  "Plot": "The last surviving child of the Russian Royal Family joins two con men to reunite with her grandmother, the Dowager Empress, while the undead Rasputin seeks her death.",
  "Language": "English, Russian, French",
  "Country": "USA",
  "Awards": "Nominated for 2 Oscars. Another 10 wins & 21 nominations.",
  "Poster": "https:\/\/images-na.ssl-images-amazon.com\/images\/M\/MV5BNGJiNWFlYTMtZTBiZi00ZTVmLWJmZmMtNzEzYzZjNzYzZmRmXkEyXkFqcGdeQXVyNTA4NzY1MzY@._V1_SX300.jpg",
  "Ratings": [
    {
      "Source": "Internet Movie Database",
      "Value": "7.1\/10"
    },
    {
      "Source": "Rotten Tomatoes",
      "Value": "85%"
    },
    {
      "Source": "Metacritic",
      "Value": "59\/100"
    }
  ],
  "Metascore": "59",
  "imdbRating": "7.1",
  "imdbVotes": "94,074",
  "imdbID": "tt0118617",
  "Type": "movie",
  "DVD": "16 Nov 1999",
  "BoxOffice": "N\/A",
  "Production": "20th Century Fox",
  "Website": "N\/A",
  "Response": "True"
}

3 Answers 3

1

The Rotten tomatoes value is in the 3rd element of the array. Array indexes start at 0. Therefore, what you need is body.Ratings[2].Value.

Sign up to request clarification or add additional context in comments.

1 Comment

If you are unsure which index Rotten Tomatoes will occupy you can do body.Rating.find((element) => element.Source === "RottenTomatoes") which will look at each index and return the first one where Source === "RottenTomatoes"
1

If the order of the Ratings array is unpredictable, use filter function as below.

    //If the order of the source array is unpredictable
    //use filter

    var rtValue = body.Ratings.filter(function(source) {
      return source.Source === "Rotten Tomatoes";
    }).Value;

Comments

0

That's because it is an array inside an object. body.Ratings[0]. You are referencing the spot of an array. Definitely look into arrays.

 "Ratings": [ { "Source": "Internet Movie Database", "Value": "7.1/10" }, { "Source": "Rotten Tomatoes", "Value": "85%" }, { "Source": "Metacritic", "Value": "59/100" } ]

See the [ square brackets ] Each object inside {} can be referenced with an integer inside [0], [1] etc. Look at some online tutorials. www.w3schools.com/js is a great place to start

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.