John John - 5 months ago 30
Javascript Question

Uncaught SyntaxError: Unexpected token p in JSON at position 36

I´m populating webpage with sharepoint so I do a json to get data with ajax like these:

function completeFleet(data, target, eng) {
var items = data.d.results;
console.log(items);
var prefix = "<div class='row'>";
var sufix = "</div>";
var menu = "<div class='col-md-4'>";
var cat = "";
var spec = "";
var counter = 0;
var obj = null;
for (item in items) {
spec = "";
if (counter == 1) {

menu += "</div><div class='col-md-4'>";
counter = 0;
}
if (eng) {
obj = JSON.parse(items[item].Specifications);
for (var key in obj) {
spec += "<div class='row'><div class='col-md-12 ftBottomSeparator'><span class=' t10'>" + key + "</span>&nbsp;<span class='t06' style='float:right;'>" + obj[key] + "</span></div></div>";
}
menu += "<div class='row ftContainerOut'><div class='col-md-12 ftContainer'><div class='row ftHeader'><div class='col-xs-9 t09'>" + items[item].Title + "</div><div class='col-xs-3 text-right'></div></div><div class='row'><div class='col-md-6' style='padding-top:10px'><img src='" + items[item].Imagen.Url + "' class='img-responsive img-center' style='border:0px solid blue; max-width:150px;max-height:120px;' /></div><div class='col-md-6'>" + spec + "</div></div></div></div>";
} else {
obj = JSON.parse(items[item].Especificaciones);
for (var key in obj) {
spec += "<div class='row'><div class='col-md-12 ftBottomSeparator'><span class=' t10'>" + key + "</span>&nbsp;<span class='t06' style='float:right;'>" + obj[key] + "</span></div></div>";
}

menu += "<div class='row ftContainerOut'><div class='col-md-12 ftContainer'><div class='row ftHeader'><div class='col-xs-9 t09'>" + items[item].Title + "</div><div class='col-xs-3 text-right'></div></div><div class='row'><div class='col-md-6' style='padding-top:10px'><img src='" + items[item].Imagen.Url + "' class='img-responsive img-center' style='border:0px solid blue; max-width:150px;max-height:120px;' /></div><div class='col-md-6'>" + spec + "</div></div></div></div>";
}
counter++;
}
$(target).html("<div class='panel-body'><div class='container-fluid'>" + prefix + menu + sufix + "</div></div>");

}


I have 5 objects different, but one of these don´t show data, my webpage is in english and spanish, in english it charge all data, but in spanish one of these
doesn´t works and I get error at position 36, and position 36 is the item don´t show. Any idea what is wrong here? Regards

Error image

These one works
Works

and this no works

noworks

---------Update------------

If I comment this line:

//obj = JSON.parse(items[item].Especificaciones);


and put

if(items[item].Especificaciones){
JSON.parse(items[item].Especificaciones);
}


it now runs with image, but now I don´t have my "Especificaciones" lists

Now when I use

var stringifyObj = JSON.stringify(items[item].Especificaciones);
var obj = JSON.parse(stringifyObj);


I get something like these:

Keyword

Answer

make sure value is not null for the corresponding key inside JSON.parse. For example-

JSON.parse(items[item].Specifications)

make sure items have value in item index and items[item] has the property Specifications.

you can check if items[item].Specifications is not null before JSON.parse.

if(items[item].Specifications){
  JSON.parse(items[item].Specifications)
}

Update

JSON.parse() is used to convert a string containing JSON notation into a Javascript object. To be valid JSON, strings must be in double quotes.

Try stringify the object and then parse again.

var stringifyObj = JSON.stringify(items[item].Especificaciones); 
var obj = JSON.parse(stringifyObj);

The reason for the error is that JSON.parse() expects a String value and items[item].Especificaciones is an Array