JSON access variable field

1.8k views Asked by At

I'm trying to write a library for Riot (League of legends API) in NodeJS and I have the following problem.

I'm doing:

function getSummonerProfile(sum) {
    var summoner = sum.replace(/\s+/g, '');

    request("https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/"
            + summoner + "?api_key=" + api_key, function(error, response, body) {
        console.log(body);
    });
}

getSummonerProfile("some player");

the console shows:

{
    "player": {
        "id": 37842773,
        "name": "player",
        "profileIconId": 548,
        "summonerLevel": 30,
        "revisionDate": 1368783726000
    }
}

Now "player" is a variable (function parameter); how can I access the data? to get for example, only the id.

body.summonner and body["player"] throw undefined.

EDIT 1 (complete code):

var request = require("request");
var api_key = 'example';

function getSummonerProfile(sum) {
    var summoner = sum.replace(/\s+/g, '');

    request("https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/"
            + summoner + "?api_key=" + api_key, function(error, response, body) {
        console.log(body.summoner.id);
    });
}

getSummonerProfile("some player");
5

There are 5 answers

7
Lajos Veres On BEST ANSWER

Your function getSummonerProfile only sends a request, using this request function which seems to be processing the internal part (console.log) when the request return with the data from server side. So basically you should save the data instead of the console.log line. You could bind it somewhere or save to a variable. And it seems that player is the body variable's key you are looking for.

Maybe something like this:

var request = require("request");
var api_key = 'example';
var summoners = {};

function getSummonerProfile(sum) {
    var summoner = sum.replace(/\s+/g, '');

    request("https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/" + summoner + "?api_key=" + api_key, function(error, response, body) {
        summoners[summoner]=JSON.parse(body);
        console.log(summoners[summoner].player.id);
    });
}

getSummonerProfile("some player");
1
aleberguer On

if you have a variable like this

json_var = {
    "player": {
        "id": 37842773,
        "name": "player",
        "profileIconId": 548,
        "summonerLevel": 30,
        "revisionDate": 1368783726000
    }
}

you can do this to access the data

id = json_var["player"]["id"]
1
TNTanuki On

What you are trying to do is JSON deserialization.

If you are using javascript, your question has an answer here: deserialize from json to javascript object

If you are using another language, the principle is the same, you need to have a class that matches the fields of your JSON file and use a deserializer.

0
AudioBubble On

Are you actually getting a JavaScript Object? You're probably getting a JSON string, you need to parse it back into a JavaScript Object...

var newdata = JSON.parse(body);
console.log(newdata.player.id);

Support is no worry, according to caniuse.com

0
Johan Karlsson On

You are getting the response as a jsonstring. To get response as js object try this:

request({url: "https://na.api.pvp.net/api/lol/na/v1.4/summoner/by-name/" + summoner + "?api_key=" + api_key, json: true}, function(error, response, body) {
        console.log(body.player.id);
    });
}