JSON – not all fields quoted in Dojo diji.tree sample code in book

dojojson

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

Best Answer

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?