1

I have a JSON Object which is dynamically created based on user interaction, the dynamic key will change every time

example JSON

{
"normalkey": "John Smith",
"dynamickey ": "testing"
}

is there any way to assign the dynamic key name without knowing the key name for example.

$.ajax({
    type: "POST",
    url: ROOT+"controlpanel/menuBuilder",
    data: "clicked="+menu,
    dataType:"json",
    success: function(data)
    {   
        $('#foo').html(data.normalKey); 
        $('#bar').html(data.dynamicKey); 

    }
user3524311
  • 25
  • 1
  • 7
  • 2
    Duplicate of http://stackoverflow.com/questions/10311361/accessing-json-object-keys-having-spaces – Rohan Kumar Apr 30 '14 at 06:22
  • 2
    possible duplicate of [JavaScript property access: dot notation vs. brackets?](http://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets) – Phil Apr 30 '14 at 06:22
  • 1
    not a duplicate of the above, this isnt a white space related issue – user3524311 Apr 30 '14 at 06:27

2 Answers2

2

when space come in name then use [] Bracket notation pattern

success: function(data)
    {   
        $('#foo').html(data["normal key"]); 
        $('#bar').html(data["dynamic key"]); 

    }

updated to get key use jQuery.each

$.each(data, function(key, item){
       console.log(key); // here you get the key names
    });
Rituraj ratan
  • 10,260
  • 8
  • 34
  • 55
  • 1
    `[]` is called [Bracket notation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Member_Operators#Bracket_notation) – Satpal Apr 30 '14 at 06:22
  • 1
    Hi, its not the space thats the issue the dynamic key will be different every time, so next time it could be dynamic key 2 for example. My question is how do i access the key without knowing its name? – user3524311 Apr 30 '14 at 06:25
1

As this is an javascript object you can use jQuery's $.each() method to get the key names:

success: function(data)
{   
    $.each(data, function(key, item){
       console.log(key); // this gets you the key names
    });
}

Demo @ Fiddle


As per your comment

how would i assign key 2 to a variable without using its name?

var o = {
    "normalkey": "John Smith",
    "dynamickey ": "testing"
};
var a = []; // declare an empty array.
$.each(o, function(key, item){
    a.push(key); // push the keys in it.
});
var dynkey = a.pop(); // .pop() gets you the dynamickey here as 
                      // this is the last item in the array.

console.log(dynkey); // this logs = dynamickey 

Another Demo @ Fiddle


Description about .pop() @ MDN Docs:

The pop method removes the last element from an array and returns that value to the caller.


So with your last comment:

success: function(data)
{   
   var a = [];
   $.each(data, function(key, item){
      a.push(key); // this gets you the key names
   });
   var dynkey = a.pop();

   $('#container').html(data[dynkey]); // <----here you have to do this.

}
Community
  • 1
  • 1
Jai
  • 74,255
  • 12
  • 74
  • 103