tags:

views:

361

answers:

2

O'Reilly book "Dojo - The Definitive Guide" page 378 shows the following sample Tree structure which is supposedly JSON. It seems to work in building the Dijit Tree structure.

{
identifier: 'name',
label:'name',
items: [
{
   name: "Programming Languages",
     children: [
etc...

Should the word identifier, label, items, name, and children be enclosed in quotes?

I'm writing a Python program to generate syntax that is compatible with their desired tree structure. Just to test my output, I tried:

testDict = "xxxx" where xxxx is the string supposed JSON string above. It always gives an error that 'identifier' is not defined.

So I'm curious if this was a typo - or if there are some new keywords or features of JSON that I need to learn.

Thanks, Neal Walters

+1  A: 

These are not new keywords or features of JSON but they are how dojo expects a JSON file to be structured. You should wrap them in quotes. Here's an example from dojocampus.

Swingley
Ok, so it was definitely a typo in the book then. Thanks.
NealWalters
Apparently however Dojo doesn't care about the quotes.I would imagine they would "eval" the JSON into a tree structure, so not sure how it works...Here's another example they provide in themetester... { identifier: 'id', label: 'name', items: [ { id: 'AF', name:'Africa', type:'continent', population:'900 million', area: '30,221,532 sq km', timezone: '-1 UTC to +4 UTC', children:[{_reference:'EG'}, {_reference:'KE'}, {_reference:'SD'}] }, { id: 'EG', name:'Egypt', type:'country' }, { id: 'KE', name:'Kenya', type:'country',
NealWalters
+2  A: 

JSON doesn't really have any additional features. That's the beauty of it :)

You don't have to wrap those names in quotes. The names before the colon are supposed to be quoted, strictly according to the JSON spec. Why? Mostly (only?) because JavaScript gets upset when reserved words are used as object properties -- for example, if you had properties called 'function' or 'return'. Quoting these names consistently avoids this problem. Dojo doesn't care. It just uses eval to parse the JSON, and as long as you avoid keywords, it won't enforce the use of quotes. You can use quotes consistently if you like to be JSON compliant.

I'm not sure exactly what problem you had with your testDict example. I don't fully understand the context (what is testDict, what language are you using to set up that string, how is it used, etc.) Perhaps you needed to escape something in the JSON such as nested double quotes?

peller
I was using the Python "eval" command to try to take the JSON string and load into a dictionary to prove that the syntax was correct.
NealWalters
Python would have an issue with missing quotes.
Kathy Van Stone
Dojo is looking into better compliance on this issue also.
peller