Converting JSON Blob

With the new API it is necessary to be able to change the username into an id for later calls. I have a script that nearly accomplishes this and is able to get the channel info as a JSON blob and my last step would be able to call the id from this blob into a variable.

     `function createId() {
                    var channelRequest = prompt("what channel do you want to watch", 'esl_csgo');
		var httpRequest = new XMLHttpRequest();
		httpRequest.addEventListener('load', filterId);
		var idLink = '' + channelRequest;'GET', idLink);
		httpRequest.setRequestHeader('Client-ID', WORKING CLIENT ID HERE');
		httpRequest.setRequestHeader('Accept', 'application/vnd.twitchtv.v5+json');

		function filterId() {
			var idInfo = JSON.parse(httpRequest.responseText);

I understand that its necessary to put the blob into an object, but I’m not good at this. The JSON blob for channel esl_csgo would be:


Any ideas?

I figured it out, turns out the parse releases as an array with objects within so to call id all you would have to do is

id = (obj.users[0]._id).

I won’t release it unless anyone wants to, but I have a script that converts usernames to id’s if that is useful for anyone here.

1 Like

Here’s a more flexible way to do calls in the future, sticking to fairly vanilla code. It’s made to work similar to the “request” Node.js module.

var clientID = 'Client ID here';

// Base for making requests
function request(opts, callback) {
	var req = new XMLHttpRequest();
	var url = opts.url;
	// Prepended URL part
	if('baseUrl' in opts && opts.baseUrl) {
		url = opts.baseUrl + url;
	var qs = (opts.qs || {});
	var qsKeys = Object.keys(qs);
	// Add query string
	if(qsKeys.length) {
		var querystring = qsKeys
			.map(function(key) {
				return [ key, qs[key] ]
					.map(function(n) { return encodeURIComponent(n); })
		url += '?' + querystring;
	var method = opts.method || 'get';, url, true);
	var headers = opts.headers || {};
	var headersKeys = Object.keys(headers);
	// Add headers
	if(headersKeys.length) {
		headersKeys.forEach(function(key) {
			req.setRequestHeader(key, headers[key]);
	if(callback) {
		req.addEventListener('load', function() {
				let data = this.responseText;
				// Try to parse the JSON if asked for
				if(opts.json) {
					try {
						data = JSON.parse(this.responseText);
					} catch(err) {
						callback(err, this);
				callback(null, data);
		req.addEventListener('error', function(err) {
				callback(err, this); // Not quite sure how XMLHTTPRequest errors work
	return req;

function kraken(opts, callback) {
	// Assign default Twitch Kraken API base
	if('baseUrl' in opts === false) {
		opts.baseUrl = '';
	// Assign default headers
	opts.headers = Object.assign({
			Accept: 'application/vnd.twitchtv.v5+json',
			'Client-ID': clientID,
		}, opts.headers || {});
	// Use JSON
	opts.json = true;
	return request(opts, callback);

So how can you use this? Create specific-use functions to make calls simple and clear in the rest of the code.

Key     | Type    | Description
url     | string  | The URL, or partial URL to request
baseUrl | string  | A part of the URL to be prepended to url
qs      | object  | A key/value representation of the request's querystring
headers | object  | A key/value representation of the request's headers
json    | boolean | Whether or not to attempt to parse as JSON

Use kraken like a default-option base function and pass just the new parts.

// Get the user object
function usernameToUserObject(username, callback) {
	return kraken({
		url: 'users',
		qs: { login: username }
	}, function(err, data) {
		if(err) {
			callback(err, data);
		else if(data.users.length > 0) {
			callback(err, data.users[0]);
		else {
			callback(err, null);

// Get just the "_id" key
function usernameToID(username, callback) {
	return usernameToUserObject(username, function(err, data) {
		if(err) {
			callback(err, data);
		else if(data !== null) {
			callback(err, data._id);

In action:

// Will print "7676884" to the console
var req = usernameToID('alca', function(err, data) {
	if(err !== null) {

The problem for me would be it isn’t entirely vanilla code. I’m planning on launching my web application from a browser called WPE which is notorious for having terrible support. It doesnt even support iframe well for an example… Luckily my script works well enough, but thx for the help !

Well if I knew that I’d use even older strategies xD I’m interested though, what devices are you targeting with this app?

A set up box called EOS that will be released in Belgium. I’m an intern for liberty global and was hoping to implement a twitch streaming application.

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