JSON: Array of object literals?

K

Keith Hughitt

Hi all,

I've been testing out the native JSON support in Firefox 3.5, and have
run into some issues. I was wondering if someone might be able to help
me figure out what is going on.

I would like to be able to encode and decode an array of JavaScript
object literals, however, the parser doesn't seem to like having an
array as the outermost container, e.g.:

// Okay
JSON.parse(JSON.stringify({name: "bob",id: 3}));

// Okay
JSON.parse(JSON.stringify({objects: ["bob", 3]}));

// Not Okay
JSON.parse(JSON.stringify([{name: "bob",id: 3}, {name: "john",id:
4}]));

// Doesn't work, either
JSON.parse(JSON.stringify([1,2,3]));

All of the above should be valid JSON. So why doesn't the above work?

Any feedback would be greatly appreciated.

Thanks!
Keith
 
K

Keith

Update: Works in Prototype & jQuery (with JSON plugin).

For example:

//Okay
Object.toJSON([{name: "bob",id: 3}, {name: "john",id:4}]).evalJSON
();

// Also okay
$.evalJSON($.toJSON([{name: "bob",id: 3}, {name: "john",id:4}]));

Still need to test json2.js. For now though it looks like this problem
is restricted primarily to Firefox's native JSON support :\
 
T

Thomas 'PointedEars' Lahn

Keith said:
[native JSON support in Firefox 3.5]
the parser doesn't seem to like having an array as the outermost
container, e.g.:

// Okay
JSON.parse(JSON.stringify({name: "bob",id: 3}));

// Okay
JSON.parse(JSON.stringify({objects: ["bob", 3]}));

// Not Okay
JSON.parse(JSON.stringify([{name: "bob",id: 3}, {name: "john",id:
4}]));

// Doesn't work, either
JSON.parse(JSON.stringify([1,2,3]));

All of the above should be valid JSON. So why doesn't the above work?

<http://jibbering.com/faq/#posting>
<http://jibbering.com/faq/faq_notes/clj_posts.html#ps1DontWork>
<http://catb.org/~esr/faqs/smart-questions.html>
<http://catb.org/~esr/faqs/smart-questions.html#examples>


PointedEars
 
T

Thomas 'PointedEars' Lahn

Keith said:
Update: Works in Prototype & jQuery (with JSON plugin).

So what? There are other, more important things that don't even remotely
work there.


PointedEars
 
J

Jorge

Keith said:
(...)

// Not Okay
JSON.parse(JSON.stringify([{name: "bob",id: 3}, {name: "john",id:
4}]));

// Doesn't work, either
JSON.parse(JSON.stringify([1,2,3]));

All of the above should be valid JSON. So why doesn't the above work?

Any feedback would be greatly appreciated.
JSON.parse(JSON.stringify([{name: "bob",id: 3}, {name:
"john",id:4}]))[1]['name'];
--> "john"
JSON.parse(JSON.stringify([1,2,3]));
--> [1, 2, 3]

Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.1pre)
Gecko/20090701 Shiretoko/3.5.1pre
 
E

Eric Bednarz

Keith Hughitt said:
I've been testing out the native JSON support in Firefox 3.5,

Yesterday’s official release or a preview version, what platform, etc?
I would like to be able to encode and decode an array of JavaScript
object literals, however, the parser doesn't seem to like having an
array as the outermost container,

Define “doesn't seem to likeâ€, as in what outcome do you expect and what
outcome do you observe in what context.
e.g.:

// Okay
JSON.parse(JSON.stringify({name: "bob",id: 3}));

// Okay
JSON.parse(JSON.stringify({objects: ["bob", 3]}));

// Not Okay
JSON.parse(JSON.stringify([{name: "bob",id: 3}, {name: "john",id:
4}]));

// Doesn't work, either
JSON.parse(JSON.stringify([1,2,3]));

All of the above should be valid JSON.

All of the above works for me. Bummer :)
So why doesn't the above work?

So far you are the only one with enough information to find the answer
to that question. Define the environment you are using and provide a
complete test case.
 
W

wilq

Hi all,

I've been testing out the native JSON support in Firefox 3.5, and have
run into some issues. I was wondering if someone might be able to help
me figure out what is going on.

I would like to be able to encode and decode an array of JavaScript
object literals, however, the parser doesn't seem to like having an
array as the outermost container, e.g.:

    // Okay
    JSON.parse(JSON.stringify({name: "bob",id: 3}));

    // Okay
    JSON.parse(JSON.stringify({objects: ["bob", 3]}));

    // Not Okay
    JSON.parse(JSON.stringify([{name: "bob",id: 3}, {name: "john",id:
4}]));

    // Doesn't work, either
    JSON.parse(JSON.stringify([1,2,3]));

All of the above should be valid JSON. So why doesn't the above work?

Any feedback would be greatly appreciated.

Thanks!
Keith

I'm not sure if thats the case, but from what I remember JSON is more
strict about names of object attribute. Try instead of doing name:
"bob" to do "name": "bob" . Does that help ?
 
K

Keith

Hi all,

Thanks for taking the time to test the code snippets I posted, and
replying. Sorry I didn't provide more information in the original post
regarding the environment I ran the code in. Here it is now:

Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.1pre) Gecko/
20090731 Shiretoko/3.5.1pre
Firebug 1.4.0b3
Prototype 1.6.0.3
jQuery 1.3.2

The code snippets were run in Firebug, and also in small test pages
where the output was simply dumped into a container on the screen.

In all of the above, the goal was to be able to successfully encode
(stringify) and then decode (parse) an array of object literals of the
form:

[
{
name: "bob",
id: 3
},
{
name: "john",
id: 4
}
]

So the expected output is a JavaScript object (exactly the same as the
input), and the output I was getting instead was a string of the form:

"[{"name": "bob", "id": 3}, {"name": "john", "id": 4}]"

Finally. I did test both with and without quotes key names, which
although helping the JSON to qualify as "valid" (e.g. http://www.jsonlint.com/),
made no difference
in the above tests.

I have been able to track down the issue, and it turns out that it is
related to a conflict with Prototype (tested: 1.6.0.3 and 1.6.1rc3).
Here is a simple file that can verify the problem:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/
TR/html4/strict.dtd">
<html>
<head>
<title>Native JSON Test</title>

<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/
prototype.js" type="text/javascript"></script>

<script type="text/javascript">
var test = function () {
//console.log(JSON.parse(JSON.stringify([{"name": "bob", "id":
3}, {"name": "john", "id": 4}])));
var output = document.getElementById("output");
output.innerHTML = JSON.parse(JSON.stringify([{"name": "bob",
"id": 3}, {"name": "john", "id": 4}]));
};
</script>

</head>
<body onload="test();">
<div id="output"></div>
</body>
</html>

Run first as is and you should see:

[{"name": "bob", "id": 3}, {"name": "john", "id": 4}]

which means that the stringified JSON was not parsed correctly, and is
returning a string instead of an object.

Next, try commenting out the Prototype include and running again. You
should now see:

[object Object],[object Object]

Success! (you can verify that it is indeed the same object by running
the function in Firebug).

If someone else could verify this for me on a different setting, it
would be much appreciated. :)

Thanks all,
Keith
 
G

Gregor Kofler

Keith meinte:
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.1pre) Gecko/
20090731 Shiretoko/3.5.1pre
Firebug 1.4.0b3
Prototype 1.6.0.3
jQuery 1.3.2
[snip]

I have been able to track down the issue, and it turns out that it is
related to a conflict with Prototype (tested: 1.6.0.3 and 1.6.1rc3).

Surprise, surprise...

Gregor
 
E

Eric Bednarz

Keith said:
Sorry I didn't provide more information in the original post
regarding the environment I ran the code in. Here it is now:

Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.1pre) Gecko/
20090731 Shiretoko/3.5.1pre
Firebug 1.4.0b3
Prototype 1.6.0.3
jQuery 1.3.2

Oh well. Let’s hope you learned something from that.

(Frankly, I totally fail to understand how anyone could include huge
libraries in test cases for testing native browser features, especially
new ones, where incompatibilities haven’t been blogged about to death,
or however that works nowadays :)
Finally. I did test both with and without quotes key names, which
although helping the JSON to qualify as "valid" (e.g. http://www.jsonlint.com/),
made no difference
in the above tests.

Richard already explained why that doesn’t matter.
 
D

David Mark

Keith wrote:

[...]


Run first as is and you should see:
    [{"name": "bob", "id": 3}, {"name": "john", "id": 4}]
which means that the stringified JSON was not parsed correctly, and is
returning a string instead of an object.
Next, try commenting out the Prototype include and running again. You
should now see:
    [object Object],[object Object]
Success! (you can verify that it is indeed the same object by running
the function in Firebug).
If someone else could verify this for me on a different setting, it
would be much appreciated. :)

Yes, the reason for this is that `JSON.stringify` invokes operand's
`toJSON` method, if it's callable (see 15.12.3 in ES5-draft). Prototype
happens to define `Array#toJSON` (following implementation of earlier
versions of json.js, AFAIR).

Somebody tell them to stop doing that. Oh, that's right. :) Hard to
believe people still use that thing. It was declared DOA when it came
out and there has been no change in that condition.

[snip]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top