Hugolpz
Hugolpz

Reputation: 18248

"Uncaught SyntaxError: Unexpected token :" when there is no ":"?

Given JSON data without indexes :

var knol = [
    {"hant":"火","status":1,"rank":30,"score": 1},
    {"hant":"人","status":1,"rank":33,"score": 2}, 
    {"hant":"山","status":1,"rank":60,"score": 5}, 
    {"hant":"是","status":1,"rank":50,"score": 3}
];

Given a JS function to recreate the data with indexes :

var addIndex = function(object, key1, key2){
    var data = object;
    var newData = "[";

    for (var i in data) {
        if (!key2) {
            var newIndex = data[i][key1]
        } else {
            var newIndex = data[i][key1] + data[i][key2]
        }

        newData = newData + "\"" + newIndex + "\":" + JSON.stringify(data[i]) +",";
    }

    newData = newData.concat("]").replace(",]", "]");
    console.log(newData);
    newData = JSON.parse(newData);
    return newData;
};

knol = addIndex(knol, "hant");

I get the error "Uncaught SyntaxError: Unexpected token :", while there is NO ":" at the indicated place in the code. This seems to crash my larger script. Any idea what is going on ?

Fiddle : http://jsfiddle.net/n9pf6/2/

Upvotes: 0

Views: 98

Answers (2)

user2864740
user2864740

Reputation: 61865

The intermediate JSON is incorrect, so the final JSON.parse is failing. The intermediate text is ["火":{"hant":"火",.., as reported on the console, and it is indeed incorrect, as arrays don't have keys.

Just build/rebuild the entire object-graph as a JS object and then use JSON.stringify once on it - or, in this case, never, as the end result will be a JS object anyway!

var addIndex = function(data, key1, key2){
    var newData = {}; // new JS object - there is /no/ JSON here.
    var newIndex;
    for(var i in data){
        if(!key2){
           newIndex = data[i][key1];
        }else{
           newIndex = data[i][key1] + data[i][key2];
        }
        // add data to new object with appropriate property/key
        // and do NOT manually build JSON text.
        newData[newIndex] = data[i];
    }
    // return object directly; it's not JSON text, no need to parse it!
    return newData;
};

Upvotes: 2

p.s.w.g
p.s.w.g

Reputation: 148980

You need to create an object ({…}), not an array ([…]). This would work:

var newData = "{";
for(var i in data){
    if(!key2){ var newIndex = data[i][key1] }else{ var newIndex = data[i][key1]+ data[i][key2]}
    newData = newData + "\""+newIndex+"\":"+JSON.stringify(data[i])+",";
    }
newData = newData.concat("}").replace(",]", "]").replace(",}", "}");

However, I'd strongly recommend just manipulating the data directly rather that trying to manually construct a JSON string from its various parts.

Here is a simple implementation:

var addIndex = function(object, key1, key2){
    var data = object, newData = {}, newIndex;
    for(var i in data){
        if(!key2){ 
            newIndex = data[i][key1];
        }else{ 
            newIndex = data[i][key1]+ data[i][key2];
        }
        newData[newIndex] = data[i];
    }
    console.log(newData);

    return newData;
};

Demonstration

Upvotes: 1

Related Questions