[HELP] Not valid Json for Brand24 Integration


#1

Hello,

I am currently working on a Brand24 Integration an I have the following error which I can not find the cause.

“cb(new Error(‘The response from the server was not valid JSON’));”

Is someone can help me please ?

Here is my code :

var XMLHttpRequest = require(‘xhr2’);
var DOMParser = require(‘xmldom’).DOMParser;

var nb = 30;
var tab = new Array();
for(var i_tab=0;i_tab<nb;i_tab++){
   tab[i_tab] = new Array();
}
var i_line = 0;
var i_column = 0;

function ajaxGet(url, callback) {
    var req = new XMLHttpRequest();
    req.open("GET", url);
    req.addEventListener("load", function () {
        if (req.status >= 200 && req.status < 400) {
            var xmlDOM = new DOMParser().parseFromString(req.responseText, 'text/xml');
            callback(xmlToJson(xmlDOM));
        } else {
            console.error(req.status + " " + req.statusText + " " + url);
        }
    });
    req.addEventListener("error", function () {
        console.error("Erreur réseau avec l'URL " + url);
    });
    req.send(null);
}

function afficher(reponse) {
	var date = new Date(reponse["results"]["date1"]);
	var day = date.getDate();
	if(day<10){
	  day="0"+day;
	}
	var month = date.getMonth()+1;
	if(month<10){
	  month="0"+month;
	}
	var year = date.getFullYear();
	tab[i_line][0] = year + "-" + month + "-" + day;
	console.log("\n\n"+tab[i_line][0]);

    var total=0;
    for(var i_category=0;i_category<Object.keys(reponse["results"]["categories"]["category"]).length;i_category++)
    {
      if(reponse["results"]["categories"]["category"][i_category]["name"] !== "Video")
      {
       var name = reponse["results"]["categories"]["category"][i_category]["name"];
       var count = reponse["results"]["categories"]["category"][i_category]["count"];
       console.log(name+"\n"+count);
       total = total + Number(count);

       tab[i_line][i_category+1] = count;
      }
    }
    tab[i_line][9] = total;

    console.log(total);
    console.log(tab[i_line][0]+" "+tab[i_line][1]+" "+tab[i_line][2]+" "+tab[i_line][3]+" "+tab[i_line][5]+" "+tab[i_line][6]+" "+tab[i_line][7]+" "+tab[i_line][8]+" "+tab[i_line][9]);
    i_line = i_line + 1;




	//Geckoboard
	var API_KEY = '';

	var gb = require('geckoboard')(
	API_KEY
	);

	gb.datasets.findOrCreate(   //Cherche ou créé le Dataset
	  {
		id: 'brand24.mentions',   //Id du Dataset
		fields: {   //Champs du dataset
		  date: {
		    type: 'date',
		    name: 'Date'
		  },
		  twitter: {
		    type: 'number',
		    name: 'Twitter'
		  },
		  instagram: {
		    type: 'number',
		    name: 'Instagram'
		  },
		  blogs: {
		    type: 'number',
		    name: 'Blogs'
		  },
		  facebook: {
		    type: 'number',
		    name: 'Facebook'
		  },
		  forum: {
		    type: 'number',
		    name: 'Forum'
		  },
		  news: {
		    type: 'number',
		    name: 'News'
		  },
		  web: {
		    type: 'number',
		    name: 'Web'
		  },
		  total: {
		    type: 'number',
		    name: 'Total'
		  }
		}
	  },

	  function (err, dataset) {
		if (err) {
		  console.error(err);
		  return;
		}

		for (var i = 0; i < nb-2; i++) {

		  var data1 = tab[i][0];
		  var data2 = Number(tab[i][1]);
		  var data3 = Number(tab[i][2]);
		  var data4 = Number(tab[i][3]);
		  var data5 = Number(tab[i][5]);
		  var data6 = Number(tab[i][6]);
		  var data7 = Number(tab[i][7]);
		  var data8 = Number(tab[i][8]);
		  var data9 = Number(tab[i][9]);

		  dataset.put(
		    [
		      { date: data1.valueOf(), twitter: data2.valueOf(), instagram: data3.valueOf(), blogs: data4.valueOf(), facebook: data5.valueOf(), forum: data6.valueOf(), news: data7.valueOf(), web: data8.valueOf(), total: data9.valueOf()
		      }
		    ],
	    	null,

		    function (err) {
		      if (err) {
		        console.error(err);
		        return;
		      }
		      console.log('Data Added.');
		    }
	  	  );
		}
	  }
	);
}


function getall() {
   var date = new Date();
   var dd = date.getDate();
   if(dd<10){
      dd="0"+dd;
   }
   var mm = date.getMonth()+1;
   if(mm<10){
      mm="0"+mm;
   }
   var yyyy = date.getFullYear();
   var hjd = new Date(yyyy + "-" + mm + "-" + dd);

   for(var i_date=0;i_date<nb; i_date++){
      ajaxGet("https://api.brand24.com/api/results-count/?key=API_KEY&sid=PROJECT_ID=" + hjd.getFullYear() + "-" + (hjd.getMonth()+1) + "-" + hjd.getDate() + "&d2=" + hjd.getFullYear() + "-" + (hjd.getMonth()+1) + "-" + hjd.getDate() + "&group=", afficher);
      var precedent = hjd.getDate();
      hjd.setDate(precedent - 1);
   }
}

getall();




function xmlToJson(xml) {

   // Create the return object
   var obj = {};

   if (xml.nodeType == 1) { // element
      // do attributes
      if (xml.attributes.length > 0) {
      obj["@attributes"] = {};
         for (var j = 0; j < xml.attributes.length; j++) {
            var attribute = xml.attributes.item(j);
            obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
         }
      }
   } else if (xml.nodeType == 3) { // text
      obj = xml.nodeValue;
   }

   // do children
   // If just one text node inside
   if (xml.hasChildNodes() && xml.childNodes.length === 1 && xml.childNodes[0].nodeType === 3) {
      obj = xml.childNodes[0].nodeValue;
   }
   else if (xml.hasChildNodes()) {
      for(var i = 0; i < xml.childNodes.length; i++) {
         var item = xml.childNodes.item(i);
         var nodeName = item.nodeName;
         if (typeof(obj[nodeName]) == "undefined") {
            obj[nodeName] = xmlToJson(item);
         } else {
            if (typeof(obj[nodeName].push) == "undefined") {
               var old = obj[nodeName];
               obj[nodeName] = [];
               obj[nodeName].push(old);
            }
            obj[nodeName].push(xmlToJson(item));
         }
      }
   }
   return obj;
}

Thanks !


#2

Hi there Antoine,

Sorry for the delay in getting back to you about this!

Would you be able to share an example of the payload that is generated by your code? In particular, I’m looking for the results of this

for (var i = 0; i < nb-2; i++) {

		  var data1 = tab[i][0];
		  var data2 = Number(tab[i][1]);
		  var data3 = Number(tab[i][2]);
		  var data4 = Number(tab[i][3]);
		  var data5 = Number(tab[i][5]);
		  var data6 = Number(tab[i][6]);
		  var data7 = Number(tab[i][7]);
		  var data8 = Number(tab[i][8]);
		  var data9 = Number(tab[i][9]);

		  dataset.put(
		    [
		      { date: data1.valueOf(), twitter: data2.valueOf(), instagram: data3.valueOf(), blogs: data4.valueOf(), facebook: data5.valueOf(), forum: data6.valueOf(), news: data7.valueOf(), web: data8.valueOf(), total: data9.valueOf()
		      }
		    ],
	    	null,

I’d like to see what we’re passing to the dataset in that PUT request when you run this code to collect the values. If you’d prefer not to share that in this forum, you could email it to me directly at lisa.hunt@geckoboard.com

Additionally, I think you and I spoke before about loops for adding data to datasets - at the moment, this code is set to overwrite the dataset each time. Were you instead wanting to use POST and append the data?

You’ll also likely need to pay attention to the fact that Javascript does not run in a linear way, as my colleague Jason mentioned in this previous message thread: PUT data in dataset with a loop

Lisa