user4998595
user4998595

Reputation:

Why won't JSON.parse() parse this?

I have JSON data and I am trying to parse it using JSON.parse(). I keep getting the error: unexpected token o. Could someone help? I feel I should mention that I'm going to be using the parsed JSON data to populate a Dojo store which will then be used to populate a Dijit Tree. Would anyone recommend any other method to form a tree UI?

This is my code.

$(document).ready(function(){

require([
"dojo/ready", "dojo/_base/window", "dojo/json","dojo/dom","dojo/store/Memory", "dojo/store/Observable",
"dijit/tree/ObjectStoreModel", "dijit/Tree", "dojo/domReady!", "dojo/mouse","dojo/text!./data/all.json"],
 function(ready, win, JSON, dom, Memory, Observable, ObjectStoreModel, Tree, mouse, data){

var testStore = new Memory({

    data :  JSON.parse($("#getData").click(
            function(){
                  var inputString = "pspTreegetData{}";
                  var state =  Function that executes Tcl script and returns the JSON data
                  return state;
            })),

This is my Tcl script which gives the raw JSON data when I check its output,

proc pspTreegetData {} {
            set fo [open "C:/Path/To/File/sampleTest.json" r]
            set text [read $fo]
            close $fo
            puts $text 
            return $text
}

My Json file is as follows,

 [
{
    "name" : "root",
    "id"   : "rootNode",
    "description" : "This is the root node"
},

{
    "name"  : "level1_node1",
    "id"    : "l1n1", 
    "description" : "This is the first node of level 1",
    "parent" : "rootNode"
},

    { 
        "name"  : "level2_node1",
        "id"    :  "l2n1",
        "description" : "This is the first node of level 2",
        "parent" : "l1n1"
    },

    { 
        "name"  : "level2_node2",
        "id"    : "l2n2",
        "description" : "This is the second node of level 2",
        "parent" : "l1n1"
    },

        { 
            "name"    : "level3_node1",
            "id"      : "l3n1",
            "description" : "This is the first node of level 3",
            "parent" : "l2n2"
        },

        {
            "name"    : "level3_node2",
            "id"      : "l3n2",
            "description" : "This is the second node of level 3",
            "parent" : "l2n2"
        },

{ 
        "name"  : "level1_node2",
        "id"    : "l1n2",
        "description" : "This is the second node of level 1",
        "parent" : "rootNode"
},

{ 
        "name"  : "level1_node3",
        "id"    :  "l1n3",
        "description" : "This is the third node of level 1",
        "parent" : "rootNode"
},
        { 
            "name"  : "level2_node3",
            "id"    : "l2n3",
            "description" : "This is the third node of level 2",
            "parent" : "l1n3"
        },

        { 
            "name"  : "level2_node4",
            "id"    : "l2n4",
            "description" : "This is the fourth node of level 2",
            "parent" : "l1n3"
        },


{ 
        "name"  : "level1_node4",
        "id"   : "l1n4",
        "description" : "This is the fourth node of level 1",
        "parent" : "rootNode"
}
]

Upvotes: 2

Views: 778

Answers (2)

dfsq
dfsq

Reputation: 193261

I keep getting the error : unexpected token o.

It means that you are trying to parse already parsed object. Just remove JSON.parse() and use input data as is.

Why "token o"? Because JSON.parse() runs toString() method on the provided input to make sure it is indeed of a String type. However when invoked on the object toString() produces a string "[object Object]". The first character looks like array however the second one is definitely not something parsable. Hence the error.

UPD. You are seriously confusing things when you are trying to parse jQuery instance object - because this is what happens when you write JSON.parse($("#getData").click());. Despite of the return state line inside event object, you can't return from event listener function, because it's just doesn't make any sense. You bind event, it can happen in 5 minutes - of course script won't wait until it happens.

I'm not sure how exactly you load data, you only provided one line:

var state =  Function that executes Tcl script and returns the JSON data 

but I'm pretty sure that "Function that executes Tcl script" either accepts callback function or returns thenable/promise object. In which case your code should look something like this:

require([
    "dojo/ready", "dojo/_base/window", "dojo/json", "dojo/dom", "dojo/store/Memory",
    "dojo/store/Observable", "dijit/tree/ObjectStoreModel", "dijit/Tree", "dojo/domReady!",
    "dojo/mouse", "dojo/text!./data/all.json"
], function(ready, win, JSON, dom, Memory, Observable, ObjectStoreModel, Tree, mouse, data) {

    $("#getData").click(function() {
        var inputString = "pspTreegetData{}";
        Function_that_executes_Tcl_script(function(data) {
            var testStore = new Memory({
                data: JSON.parse(data) // or if data is Object already just data: data 
            });
        })
    });

});

Upvotes: 5

ben
ben

Reputation: 3568

Actually it look like you have the classical async issue...

Something like that sounds more reasonable...

$("#getData").click(function(){
    var inputString = "pspTreegetData{}";
    var state =  Function that executes Tcl script and returns the JSON data
    var testStore = new Memory({
        data :  JSON.parse(state),
 //...

Check this post: How do I return the response from an asynchronous call?

Upvotes: 0

Related Questions