You can prolly sort the order if you wanted it in some kind of order but its 
not needed in my opinion!

Alex Mcauley
----- Original Message ----- 
From: "T.J. Crowder" <>
To: "Prototype &" <>
Sent: Monday, August 10, 2009 3:37 PM
Subject: [Proto-Scripty] Re: Getting keys from a JSON object returned from 
transport.responseText.evalJSON(); or for that matter any JSON object


Regardless of where the object came from (having evaluated it from a
JSON string or something else), that's what "" is for:

    var name, obj = "{'a': 1, 'b': 2}".evalJSON();
    for (name in obj) {
        alert(name + "=" + obj[name]);

...alerts "a=1" then "b=2".  The order is not guaranteed.  Cool, eh?

Speaking generally, "" iterates through the names of the
properties in the object.  Note that depending on the object in
question, some of those properties may refer to functions:

    var obj = {
        foo: function() {
        bar: 42
    var name;
    for (name in obj) {
        alert("typeof obj[" + name + "] = " + typeof obj[name]);

...alerts "obj[foo] = function" and "obj[bar] = number".

"" includes properties an object has inherited from its

    var a = ['one', 'two'];
    var name;
    for (name in a) {
        alert("typeof a[" + name + "] = " + typeof a[name]);

...will not only alert "a[0] =one" and "a[1] = 2" but also any other
enumerable properties on arrays.  In most implementations, there
aren't any other *enumerable* properties (some properties can be
marked "dontEnum", but only by the implementation, not our code), but
when you're using Prototype, there are lots -- because Prototype adds
lots of properties to the Array prototype referencing additional nifty
Array functions.  Which is why you don't use "" to loop through
array elements, there could be more than just the array elements.[1]

You can filter out the properties that are inherited from the
prototype by using hasOwnProperty:

    var a = ['one', 'two'];
    var name;
    for (name in a) {
        if (a.hasOwnProperty(name)) {
            alert("typeof a[" + name + "] = " + typeof a[name]);

...will only alert the two you expect.

All of the above is JavaScript, not Prototype; details in the spec[2].


HTH, have fun!
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available

On Aug 10, 2:54 pm, krishna81m <> wrote:
> I tried to google and search prototype API for a method that will give
> me just the keys in a JSON object.
> Is there none in Prototype and no way to know the keys?
> Regards
> Krishna

You received this message because you are subscribed to the Google Groups 
"Prototype &" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

Reply via email to