I am getting an error when trying to parse JSON:
SyntaxError: Unexpected token u in JSON at position 0(…) eFormsAtoZIndex.aspx:6558
Full Code:
Relevant Code:
$(document).ready(function() {
var rebuild = getParameterByName("rebuild");
var createdStructures = $('#AtoZContentDiv').children().length;
if ((rebuild !== undefined && rebuild !== null && rebuild.indexOf("true") === 0) || (createdStructures === 0)) {
// clean up pre-existing data
cleanUp();
// create container structure
createFormLinkContainers();
// Call SP web services to retrieve the information and create the A to Z
retrieveListData();
pleteInitialization();
} else {
try {
aggregateAll = jQuery.parseJSON($('#hdnAggregateAll').val());
console.log(jQuery.parseJSON($('#hdnAggregateAll').val()));
aggregatePersonal = jQuery.parseJSON($('#hdnAggregatePersonal').val());
aggregateBusiness = jQuery.parseJSON($('#hdnAggregateBusiness').val());
ministryAggregate = jQuery.parseJSON($('#hdnMinistryAggregate').val());
caAggregate = jQuery.parseJSON($('#hdnCAAggregate').val());
sTaxAggregate = jQuery.parseJSON($('#hdnSTaxAggregate').val());
bTaxAggregate = jQuery.parseJSON($('#hdnBTaxAggregate').val());
leTaxAggregate = jQuery.parseJSON($('#hdnLETaxAggregate').val());
} catch (err) {
console.log(err);
}
var type = getParameterByName("filter");
}
$("#tab-all").click(function() {
loadit('all');
});
$("#tab-business").click(function() {
loadit('business');
});
$(document).on('click', '#tab-personal', function(e) {
loadit('personal');
});
buildFilterMenu();
loadit('all');
});
function createJSONStructure(title, desc, index, type, formLink, documentLink, pubType, processId, ministry, ca, stax, btax, letax) {
if (desc !== undefined && desc !== null) {
desc = desc.replace(/</g, "<").replace(/>/g, ">");
} else {
desc = "";
}
var typeArr = [];
type = type.replace(/&/, "&");
var tempType = type.split("&");
for (i = 0; i < tempType.length; i++) {
typeArr.push(tempType[i].trim());
}
if (formLink === undefined || formLink === null || formLink.length === 0) {
formLink = "";
}
if (documentLink === undefined || documentLink === null || documentLink.length === 0) {
documentLink = "";
}
// subject, business and life event taxonomies must cater for multiple entries
var staxStructure = buildTaxonomyJSONStructure(stax, "stax");
var btaxStructure = buildTaxonomyJSONStructure(btax, "btax");
var letaxStructure = buildTaxonomyJSONStructure(letax, "letax");
var json = {
'name': title,
'desc': desc,
'type': typeArr,
'pubType': pubType,
'pdflink': documentLink.split(",")[0].replace(/\'/g, "'"),
'formlink': formLink.split(",")[0].replace(/\'/g, "'"),
'processid': processId,
'index': index,
'ministry': ministry.replace(/\,/g, " "),
'ca': ca.replace(/\,/g, " "),
'stax': staxStructure,
'btax': btaxStructure,
'letax': letaxStructure
};
return json;
}
function pleteInitialization() {
if (checkDataLoaded()) {
// add the Navigation to the containers once all the data is inserted
addNavigationToContainers();
var type = getParameterByName("filter");
if (type == null || type.length == 0) {
type = "all";
}
loadit(type);
buildFilterMenu();
filter(type);
$('#hdnAggregateAll').val(stringify(aggregateAll));
console.log(aggregateAll);
$('#hdnAggregatePersonal').val(stringify(aggregatePersonal));
$('#hdnAggregateBusiness').val(stringify(aggregateBusiness));
$('#hdnMinistryAggregate').val(stringify(ministryAggregate));
$('#hdnCAAggregate').val(stringify(caAggregate));
$('#hdnSTaxAggregate').val(stringify(sTaxAggregate));
$('#hdnBTaxAggregate').val(stringify(bTaxAggregate));
$('#hdnLETaxAggregate').val(stringify(leTaxAggregate));
} else {
retryCount += 1;
// Check that the maximum retries have not been exceeded
if (retryCount <= maxRetries) {
setTimeout("pleteInitialization();", 1000 * retryCount);
}
}
}
Can anyone point out what is wrong with the JSON structure or JS or how I can debug items within it?
EDIT (As per Jaromanda X's and CH Buckingham reply):
$('#hdnAggregateAll').val(JSON.stringify(aggregateAll));
console.log(aggregateAll); $('#hdnAggregatePersonal').val(JSON.stringify(aggregatePersonal)); $('#hdnAggregateBusiness').val(JSON.stringify(aggregateBusiness)); $('#hdnMinistryAggregate').val(JSON.stringify(ministryAggregate));
$('#hdnCAAggregate').val(JSON.stringify(caAggregate));
$('#hdnSTaxAggregate').val(JSON.stringify(sTaxAggregate));
$('#hdnBTaxAggregate').val(JSON.stringify(bTaxAggregate));
$('#hdnLETaxAggregate').val(JSON.stringify(leTaxAggregate));
ERROR:
10:42:24.274 TypeError: item is undefined
createFormLinks/<()eformsAtoZIndex.aspx:5644
.each()jquery-1.11.1.min.js:2
createFormLinks()eformsAtoZIndex.aspx:5638
processResult()eformsAtoZIndex.aspx:5507
m.Callbacks/j()jquery-1.11.1.min.js:2
m.Callbacks/k.fireWith()jquery-1.11.1.min.js:2
x()jquery-1.11.1.min.js:4
.send/b()jquery-1.11.1.min.js:4
1eformsAtoZIndex.aspx:5644:1
On line:
if (item.processid !== "0")
In Block:
function createFormLinks(formItems, index)
{
// create all links on the page and add them to the AtoZContent div for now
var parentContainer = $("#AtoZContentDiv");
if (parentContainer === null)
{
// if it doesn't exist, we exist cause I can't reliably add a new control to the body and get the display
// location correct
return;
}
// sort form link array first
formItems = sortResults(formItems, 'name', true);
var count = 0;
$.each(formItems, function(i, item)
{
var link;
count = count + 1;
//add links to parent container
if (item.processid !== "0")
{
link = item.formlink;
}
else if (item.pdflink !== "")
{
link = item.pdflink;
}
var container = $("#AtoZContent-" + index);
var itemType = "all";
if (item.type !== null && item.type !== undefined && item.type.length === 1) itemType = item.type[0];
var str = "<div id='divFormLink-" + index + "-" + count + "' type='" + itemType + "' ";
if (item.name !== undefined && item.name !== null)
{
str = str + " ministry='" + stripPunctuation(item.ministry) + "' ";
str = str + " ca='" + stripPunctuation(item.ca) + "' ";
// now, we need to handle these differently since they can have multiple values
str = str + " stax='";
for (i = 0; i < item.stax.length; i++)
{
str = str + stripPunctuation(item.stax[i]);
}
str = str + "' ";
str = str + " btax='";
for(i = 0; i < item.btax.length; i++)
{
str = str + stripPunctuation(item.btax[i]);
}
str = str + "' ";
str = str + " letax='";
for(i = 0; i < item.letax.length; i++)
{
str = str + stripPunctuation(item.letax[i]);
}
str = str + "' ";
}
str = str + " index='" + index + "' style='word-wrap: break-word;'></div>";
container.append(str);
var innerDiv = $("#divFormLink-" + index + "-" + count);
appendIcon(innerDiv, item.pubType);
innerDiv.append("<a id='formLink-" + index + "-" + count + "' href='" + link + "'>" + item.name + "</a>");
innerDiv.append("<div id='formDesc-" + index + "-" + count + "'>" + item.desc + "</div><br />");
});
}
I am getting an error when trying to parse JSON:
SyntaxError: Unexpected token u in JSON at position 0(…) eFormsAtoZIndex.aspx:6558
Full Code: http://pastebin./LXpJN8GF
Relevant Code:
$(document).ready(function() {
var rebuild = getParameterByName("rebuild");
var createdStructures = $('#AtoZContentDiv').children().length;
if ((rebuild !== undefined && rebuild !== null && rebuild.indexOf("true") === 0) || (createdStructures === 0)) {
// clean up pre-existing data
cleanUp();
// create container structure
createFormLinkContainers();
// Call SP web services to retrieve the information and create the A to Z
retrieveListData();
pleteInitialization();
} else {
try {
aggregateAll = jQuery.parseJSON($('#hdnAggregateAll').val());
console.log(jQuery.parseJSON($('#hdnAggregateAll').val()));
aggregatePersonal = jQuery.parseJSON($('#hdnAggregatePersonal').val());
aggregateBusiness = jQuery.parseJSON($('#hdnAggregateBusiness').val());
ministryAggregate = jQuery.parseJSON($('#hdnMinistryAggregate').val());
caAggregate = jQuery.parseJSON($('#hdnCAAggregate').val());
sTaxAggregate = jQuery.parseJSON($('#hdnSTaxAggregate').val());
bTaxAggregate = jQuery.parseJSON($('#hdnBTaxAggregate').val());
leTaxAggregate = jQuery.parseJSON($('#hdnLETaxAggregate').val());
} catch (err) {
console.log(err);
}
var type = getParameterByName("filter");
}
$("#tab-all").click(function() {
loadit('all');
});
$("#tab-business").click(function() {
loadit('business');
});
$(document).on('click', '#tab-personal', function(e) {
loadit('personal');
});
buildFilterMenu();
loadit('all');
});
function createJSONStructure(title, desc, index, type, formLink, documentLink, pubType, processId, ministry, ca, stax, btax, letax) {
if (desc !== undefined && desc !== null) {
desc = desc.replace(/</g, "<").replace(/>/g, ">");
} else {
desc = "";
}
var typeArr = [];
type = type.replace(/&/, "&");
var tempType = type.split("&");
for (i = 0; i < tempType.length; i++) {
typeArr.push(tempType[i].trim());
}
if (formLink === undefined || formLink === null || formLink.length === 0) {
formLink = "";
}
if (documentLink === undefined || documentLink === null || documentLink.length === 0) {
documentLink = "";
}
// subject, business and life event taxonomies must cater for multiple entries
var staxStructure = buildTaxonomyJSONStructure(stax, "stax");
var btaxStructure = buildTaxonomyJSONStructure(btax, "btax");
var letaxStructure = buildTaxonomyJSONStructure(letax, "letax");
var json = {
'name': title,
'desc': desc,
'type': typeArr,
'pubType': pubType,
'pdflink': documentLink.split(",")[0].replace(/\'/g, "'"),
'formlink': formLink.split(",")[0].replace(/\'/g, "'"),
'processid': processId,
'index': index,
'ministry': ministry.replace(/\,/g, " "),
'ca': ca.replace(/\,/g, " "),
'stax': staxStructure,
'btax': btaxStructure,
'letax': letaxStructure
};
return json;
}
function pleteInitialization() {
if (checkDataLoaded()) {
// add the Navigation to the containers once all the data is inserted
addNavigationToContainers();
var type = getParameterByName("filter");
if (type == null || type.length == 0) {
type = "all";
}
loadit(type);
buildFilterMenu();
filter(type);
$('#hdnAggregateAll').val(stringify(aggregateAll));
console.log(aggregateAll);
$('#hdnAggregatePersonal').val(stringify(aggregatePersonal));
$('#hdnAggregateBusiness').val(stringify(aggregateBusiness));
$('#hdnMinistryAggregate').val(stringify(ministryAggregate));
$('#hdnCAAggregate').val(stringify(caAggregate));
$('#hdnSTaxAggregate').val(stringify(sTaxAggregate));
$('#hdnBTaxAggregate').val(stringify(bTaxAggregate));
$('#hdnLETaxAggregate').val(stringify(leTaxAggregate));
} else {
retryCount += 1;
// Check that the maximum retries have not been exceeded
if (retryCount <= maxRetries) {
setTimeout("pleteInitialization();", 1000 * retryCount);
}
}
}
Can anyone point out what is wrong with the JSON structure or JS or how I can debug items within it?
EDIT (As per Jaromanda X's and CH Buckingham reply):
$('#hdnAggregateAll').val(JSON.stringify(aggregateAll));
console.log(aggregateAll); $('#hdnAggregatePersonal').val(JSON.stringify(aggregatePersonal)); $('#hdnAggregateBusiness').val(JSON.stringify(aggregateBusiness)); $('#hdnMinistryAggregate').val(JSON.stringify(ministryAggregate));
$('#hdnCAAggregate').val(JSON.stringify(caAggregate));
$('#hdnSTaxAggregate').val(JSON.stringify(sTaxAggregate));
$('#hdnBTaxAggregate').val(JSON.stringify(bTaxAggregate));
$('#hdnLETaxAggregate').val(JSON.stringify(leTaxAggregate));
ERROR:
10:42:24.274 TypeError: item is undefined
createFormLinks/<()eformsAtoZIndex.aspx:5644
.each()jquery-1.11.1.min.js:2
createFormLinks()eformsAtoZIndex.aspx:5638
processResult()eformsAtoZIndex.aspx:5507
m.Callbacks/j()jquery-1.11.1.min.js:2
m.Callbacks/k.fireWith()jquery-1.11.1.min.js:2
x()jquery-1.11.1.min.js:4
.send/b()jquery-1.11.1.min.js:4
1eformsAtoZIndex.aspx:5644:1
On line:
if (item.processid !== "0")
In Block:
function createFormLinks(formItems, index)
{
// create all links on the page and add them to the AtoZContent div for now
var parentContainer = $("#AtoZContentDiv");
if (parentContainer === null)
{
// if it doesn't exist, we exist cause I can't reliably add a new control to the body and get the display
// location correct
return;
}
// sort form link array first
formItems = sortResults(formItems, 'name', true);
var count = 0;
$.each(formItems, function(i, item)
{
var link;
count = count + 1;
//add links to parent container
if (item.processid !== "0")
{
link = item.formlink;
}
else if (item.pdflink !== "")
{
link = item.pdflink;
}
var container = $("#AtoZContent-" + index);
var itemType = "all";
if (item.type !== null && item.type !== undefined && item.type.length === 1) itemType = item.type[0];
var str = "<div id='divFormLink-" + index + "-" + count + "' type='" + itemType + "' ";
if (item.name !== undefined && item.name !== null)
{
str = str + " ministry='" + stripPunctuation(item.ministry) + "' ";
str = str + " ca='" + stripPunctuation(item.ca) + "' ";
// now, we need to handle these differently since they can have multiple values
str = str + " stax='";
for (i = 0; i < item.stax.length; i++)
{
str = str + stripPunctuation(item.stax[i]);
}
str = str + "' ";
str = str + " btax='";
for(i = 0; i < item.btax.length; i++)
{
str = str + stripPunctuation(item.btax[i]);
}
str = str + "' ";
str = str + " letax='";
for(i = 0; i < item.letax.length; i++)
{
str = str + stripPunctuation(item.letax[i]);
}
str = str + "' ";
}
str = str + " index='" + index + "' style='word-wrap: break-word;'></div>";
container.append(str);
var innerDiv = $("#divFormLink-" + index + "-" + count);
appendIcon(innerDiv, item.pubType);
innerDiv.append("<a id='formLink-" + index + "-" + count + "' href='" + link + "'>" + item.name + "</a>");
innerDiv.append("<div id='formDesc-" + index + "-" + count + "'>" + item.desc + "</div><br />");
});
}
Share
Improve this question
edited Sep 20, 2016 at 8:45
Brian
asked Sep 19, 2016 at 12:52
BrianBrian
2,05118 gold badges59 silver badges102 bronze badges
4
-
3
for one thing, if null, or a Number, or a Boolean, or a Date, or anything that isn't an Object is passed to your stringify function, it returns
undefined
- which when coerced to a string will give you that exact error when you try to parse it. Why are you plicating your stringify function if it's expected to return a JSON string? just use JSON.stringify and be done with it – Jaromanda X Commented Sep 19, 2016 at 13:38 - Agree with Jaromanda X, remove your custom stringify, use JSON.stringify. See if your problem goes away. – Cooper Buckingham Commented Sep 19, 2016 at 16:50
-
@CHBuckingham I get a type error:
TypeError: item is undefined
. See updated question for the changes I made. – Brian Commented Sep 20, 2016 at 8:44 - 2 I think you have an undefined in your array, the reason it's because in function processResult() you define the variable json inside an if statement, but you add the variable to the array in any case. Line 155 of your pastebin link. – Mario Santini Commented Sep 22, 2016 at 7:21
1 Answer
Reset to default 10 +25On the line 155 you push json
even if it's undefined.
if (pubType=="eForm" || pubType=="PDF") {
var json = createJSONStructure(title, desc, index, type.toLowerCase(), formLink, documentLink, pubType, processId, ministry, ca, stax, btax, letax);
}
formItems.push(json);
And after it your are trying to get item.processid
of undefined.
You can define variables in if-block, but in the case you should to add some validation.
$.each(formItems, function(i, item) {
var link;
count = count + 1;
if (item == null) {
return;
}
//add links to parent container
if (item.processid !== "0")
...
});