Invalid variable - json api calling

Hi there, pretty new to this but maybe someone can help.

I’ve formulated the below command for nightbot, that fetches the users last csgo faceit match details, but the command returns Invalid Variable.

The url returns json which I have then attempted to format into a sentence, the reason why I’m using “[0]” is because the json contains an array of matches, and I only need the details of the first in the array.

$(lfm = $(urlfetch json http://api.satont.ru/faceit?nick=lawsn));jlfm=JSON.parse(lfm);'lawsn's last faceit match on ${jlfm[0].map} ended ${jlfm[0].teamScore} [${jlfm[0].result}]. ${jlfm[0].kills}-${jlfm[0].deaths} KD (${jlfm[0].kd} K/D), HS (${jlfm[0].hs.percentage})')

You need to run all this as JavaScript using $(eval <javascript code>).

Each JavaScript variable has to be initialized with let or const (or var for older JS versions).

For example, your first variable, lfm, needs to be initialized with…
let lfm=$(urlfetch json http://api.satont.ru/faceit?nick=lawsn);

‘let’ allows a variable to eventually get reassigned a new value
‘const’ declares the variable contents cannot be reassigned (or ‘overwritten’)

Hope that helps with the invalid variable error!

Ok I’ve declared them correctly as show below

$(eval let lfm=$(urlfetch json http://api.satont.ru/faceit?nick=lawsn);let jlfm=JSON.parse(lfm);`lawsn's last faceit match on ${jlfm[0].map} ended ${jlfm[0].teamScore} [${jlfm[0].result}]. ${jlfm[0].kills}-${jlfm[0].deaths} KD (${jlfm[0].kd} K/D), HS (${jlfm[0].hs.percentage})`)

but the response im getting is,

Unexpected token o in JSON at position 1
1 Like

Have you checked the structure of the response?
You are trying to access the array directly, however the last matches are stored in jlfm.latestMatches
try accessing the data that way, for example: jlfm.latestMatches[0].map

Ok modified again, but still throwing this same Unexpected token error.

$(eval let lfm=$(urlfetch json http://api.satont.ru/faceit?nick=lawsn);let jlfm=JSON.parse(lfm);`lawsn's last faceit match on ${jlfm.latestMatches[0].map} ended ${jlfm.latestMatches[0].teamScore} [${jlfm.latestMatches[0].result}]. ${jlfm.latestMatches[0].kills}-${jlfm.latestMatches[0].deaths} KD (${jlfm.latestMatches[0].kd} K/D), HS (${jlfm.latestMatches[0].hs.percentage})`)

Yeah thats almost right, the urlfetch part should be inside quotes, since it’s a string. You can parse it directly without assinging a new var for it:

$(eval let jlfm=JSON.parse(`$(urlfetch json http://api.satont.ru/faceit?nick=lawsn)`); `lawsn's last faceit match on ${jlfm.latestMatches[0].map} ended ${jlfm.latestMatches[0].teamScore} [${jlfm.latestMatches[0].result}]. ${jlfm.latestMatches[0].kills}-${jlfm.latestMatches[0].deaths} KD (${jlfm.latestMatches[0].kd} K/D), HS (${jlfm.latestMatches[0].hs.percentage})`)
1 Like

Ah ok so no need to parse after. The command seems to be functioning now, just need to format the wording a little better :+1:t2: Many thanks.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.