views:

103

answers:

5

I'm getting JSON name/value pairs that looks like this:

{
           "Name":"parentid",
           "Value":"blah"
        },
        {
           "Name":"siteid",
           "Value":"blah"
        },
        {
           "Name":"sitename",
           "Value":"blah"
        }

But I would like to access the "name" value as the KEY, and the "value" value as the VALUE. Is there an elegant way to turn that piece of JSON into something like this?

{'parentid', 'blah'},

{'sitename', 'blah'}

+4  A: 

Try this:

var items = [
    {
       "Name":"parentid",
       "Value":"blah"
    },
    {
       "Name":"siteid",
       "Value":"blah"
    },
    {
       "Name":"sitename",
       "Value":"blah"
    }
];

var results = new Object();

for (var i = 0; i < items.length; i++)
{
    results[items[i].Name] = items[i].Value;
}

This will result in something like:

var results = { parentid: "Blah", siteid: "Blah", sitename: "Blah" };
AlbertEin
A: 

I'm assuming you are using PHP, and the PHP echoes you assosiatice aray like this:

echo json_encode($result);

In your javascript, you could do this:

// Soemthing retrieves php result and puts it in `var result`.
data = eval("(" + result+ ")");
alert(data.parentid);

I'm not sure if this is what you want, but it's a solution.

Steven
+1  A: 
function objectflatten (array) {
  var out = {}, i;
  for(i = 0; i < array.length; i++) {
     out[array[i].name] = array[i].value;
  }
  return out;
}

This is a function that will take an object in the form you presented, and output it as a "normal" object with the name values as keys, and the value values as values.

Breton
+1  A: 

One way to do it.

var json = [
  {
    "Name":"parentid",
    "Value":"blah"
  },
  {
    "Name":"siteid",
    "Value":"blah"
  },
  {
    "Name":"sitename",
    "Value":"blah"
  }
];

for ( var i = 0, l = json.length, obj; i < l; i++ )
{
  obj = json[i];
  json[i] = new Object();
  json[i][obj.Name] = obj.Value;
} 

// console.log() requires Firebug
console.log( json );
Peter Bailey
Instead of the Object() call, why not just: `json[i] = { obj.Name: obj.Value }`
darkporter
+1  A: 

I'd recommend using the for( ... in ... ) method for this task. It'll grab the key names like you need.

var jsonObj = eval( '([{ "Name":"parentid", "Value":"blah" }])' );

for( var i = 0, assoc = {}, key; i < jsonObj.length; ++i )
{
    for( key in jsonObj[ i ] ) // <-- this right here
    {
        assoc[ key ] = jsonObj[ i ][ key ];
    }
}

and you end up with (from Firebug)

Object Name=parentid Value=blah

that can be accessed by object.key or object[ 'key' ] (in our case assoc.Name or assoc[ 'Value' ])

here's a link from Douglas Crockford from Yahoo! about using it as well - http://yuiblog.com/blog/2006/09/26/for-in-intrigue/

Dan Beam
Be careful doing this because you could possibly be looping through things you don't know are there because they were inherited. See the "for in" section here: http://www.jslint.com/lint.html
Zack Mulgrew
you're right, to be "safer" you could use the Crock's methodfor (name in object){ if (object.hasOwnProperty(name)) { .... }}but in JSON you're parsing or eval( ) ing the object, so there shouldn't be any junk like if you were to do this to a DOM Node (try my code, inspect the result of "assoc" in Firebug's DOM tab by clicking on the return value).
Dan Beam