Ad

How Do I Specify Specific Data From An API?

- 1 answer

I am attempting to extract specific data from the Steam API so far I have been able to filter the results, here is an example;

https://store.steampowered.com/api/appdetails?appids=218620&filters=metacritic

Which gives me;

{"218620":
  {"success":true,
   "data":
     {"metacritic": 
       {"score":79,"url":"https:\/\/www.metacritic.com\/game\/pc\/payday-2?ftag=MCD-06-10aaa1f"}
     }
  }
}

However, what I want is just the numerical score, so in this instance, it would just display '79'.

I am presenting the data using an iframe which is why it needs to call the specific value. If anyone has any suggestions about how I can do this or maybe offer up an alternative way to parse the data.

My coding skills are limited so this might seem like a basic question or a rather rudimentary way to extract the data so apologies in advance. I am also adding the code to Shopify which is PHP based if that helps.

I know https://steamspy.com/api.php have their own API which offers similar data would it be easier to collect it from there instead of directly from the Steam Store?

Ad

Answer

The data you get is in JSON format. JSON is designed for easy data access.

You did not specify your programming language and I have no experience with shopify. So here it is in Javascript. Try this JSFiddle.

const jsonObject = {
  "218620":
    {"success":true,
      "data":
        {"metacritic":
            {"score":79,"url":"https:\/\/www.metacritic.com\/game\/pc\/payday-2?ftag=MCD-06-10aaa1f"}
        }
    }
};

const metacriticScore = jsonObject["218620"].data.metacritic.score;
console.log(metacriticScore);
Ad
source: stackoverflow.com
Ad