2

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"
}
]

2 Answers2

5

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 
            });
        })
    });

});
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • How is it parsed? I'm using this raw data! As I've shown you. –  Jul 03 '15 at 12:23
  • Probably because of the way you get the data (an xhr call maybe ?) – ben Jul 03 '15 at 12:24
  • No. I'm using a Tcl script that gets this data by opening the file, storing it into a variable and returning it. –  Jul 03 '15 at 12:27
  • From your code I can see that you are trying to parse jQuery instance object: `JSON.parse($("#getData"))`. In this case of course you will get above error. – dfsq Jul 03 '15 at 12:31
  • That's an event handler that returns the variable "state" which contains the JSON. I am not parsing a jQuery instance. –  Jul 03 '15 at 12:35
  • BTW, why mixing dojo and jquery ? `$(document).ready` is not needed thank to `dojo/domReady!` and `$("#getData").click` can be achieved using `dojo/on` (something like: `on(click, '#getData', function() {/*...*/})`) – ben Jul 03 '15 at 12:37
  • @Aleph `$("#getData").click(function() { /* whatever return or not */ })` is going to be instance of jQuery constructor. Check `console.log( $("#getData").click(function() {}) instanceof jQuery`. – dfsq Jul 03 '15 at 12:41
  • @ben, because I couldn't find any good tutorials or references for Dojo, and I'm comfortable with jQuery. Big mistake, I guess. –  Jul 03 '15 at 12:48
  • @Aleph did you look at dojo website ? http://dojotoolkit.org/documentation/#tutorials – ben Jul 03 '15 at 12:50
  • @dsfq, so if I use `on(click, '#getData`, function() {...}))`, as @ben says, would I get a JSON file that could be parsed? Because I'm not getting anything, to be honest. –  Jul 03 '15 at 12:52
  • @Aleph Check updated answer, I tried to explain it better. – dfsq Jul 03 '15 at 13:09
  • Thank you very much. This worked. I'm new to JavaScript. Appreciate the patience. –  Jul 03 '15 at 14:52
0

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?

Community
  • 1
  • 1
ben
  • 3,558
  • 1
  • 15
  • 28