JSON: Array of object literals?

Discussion in 'Javascript' started by Keith Hughitt, Jul 1, 2009.

  1. 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
    Keith Hughitt, Jul 1, 2009
    #1
    1. Advertising

  2. Keith Hughitt

    Keith Guest

    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 :\
    Keith, Jul 1, 2009
    #2
    1. Advertising

  3. Keith Hughitt wrote:
    > [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
    Thomas 'PointedEars' Lahn, Jul 1, 2009
    #3
  4. Keith wrote:
    > Update: Works in Prototype & jQuery (with JSON plugin).


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


    PointedEars
    Thomas 'PointedEars' Lahn, Jul 1, 2009
    #4
  5. Keith Hughitt

    Jorge Guest

    Keith Hughitt wrote:
    > (...)
    >
    > // 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

    --
    Jorge.
    Jorge, Jul 1, 2009
    #5
  6. Keith Hughitt

    Eric Bednarz Guest

    Keith Hughitt <> writes:

    > 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.
    Eric Bednarz, Jul 1, 2009
    #6
  7. Keith Hughitt

    wilq Guest

    On Jul 1, 9:53 pm, Keith Hughitt <> wrote:
    > 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 ?
    wilq, Jul 2, 2009
    #7
  8. Keith Hughitt

    Keith Guest

    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
    Keith, Jul 2, 2009
    #8
  9. 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


    --
    http://www.gregorkofler.com
    http://web.gregorkofler.com - vxJS, a JS lib in progress
    Gregor Kofler, Jul 2, 2009
    #9
  10. Keith Hughitt

    Eric Bednarz Guest

    Keith <> writes:

    > 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.
    Eric Bednarz, Jul 2, 2009
    #10
  11. Keith Hughitt

    David Mark Guest

    On Jul 2, 11:58 pm, kangax <> wrote:
    > 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]
    David Mark, Jul 3, 2009
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Goche
    Replies:
    8
    Views:
    16,426
  2. Florian Frank
    Replies:
    0
    Views:
    227
    Florian Frank
    Jun 30, 2009
  3. sajuptpm
    Replies:
    2
    Views:
    316
    sajuptpm
    Dec 28, 2012
  4. Acácio Centeno
    Replies:
    1
    Views:
    241
    dieter
    Feb 15, 2013
  5. Bryan Britten
    Replies:
    9
    Views:
    255
    Bryan Britten
    May 28, 2013
Loading...

Share This Page