0
Good evening, Folks!
I am writing a Nodejs that accesses a URL that contains a JSON in that JSON the keys contains special character "-" that is causing error in my application which would be the correct way to access them?
Example Error:
var playing = ('#Playnow: ' + parsedRadio.data.Current-music.Song);
Referenceerror: music is not defined
Example JSON:
data: {
current-music: {
singer: "NOME_BANDA",
song: "NOME_MÚSICA"
}
}
Example Code:
const request = require('request');
request('http://myplayer.myradio.com.br/tocando.php', function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
var parsedRadio = JSON.parse(body);
var tocando = ('#TocandoAgora: ' + parsedRadio.data.current-music.song);
console.log(tocando);
});
I have tried the following ways and failed.
parsedRadio.data.[current-music].song
parsedRadio.data.['current-music'].song
parsedRadio.data.'current-music'.song
parsedRadio.data.currentmusic.song
parsedRadio.data.{current-music}.song
Forgive me if it’s a basic mistake, I’m still learning Nodejs. I thank you all.
Good evening, @Andersonmendes! Thank you for answering! but I got the following error following your solution
var parsedRadio = Object.values(body.data)
 ^
TypeError: Cannot convert undefined or null to object
– J.SILVA
Extracts the body object in the same way you were doing before and then uses Object.value to pick up the values of the object.
– Chance
Tested here the way I left and really gave this mistake, Czech now, modified the answer.
– Chance
It worked, thank you!
– J.SILVA