Martti Mäger Martti Mäger - 6 months ago 27
JSON Question

Get value of a complex key with jQuery

I ran into a problem using the Pipedrive API. I tried to get some data using the below but it returned an error:

$.each(data.data, function(key,value) {
console.log(value.0d1df598a5539ab5b6b410b339dc9218e0acb091);
});


However this works:

$.each(data.data, function(key,value) {
console.log(value.person_name);
});


Why can't I get values of the keys that are complex strings generated by the Pipedrive system?

Answer

To retrieve what you require you would need to use bracket notation as the first character of the property identifier is an integer. Try this:

var value = {
    '0d1df598a5539ab5b6b410b339dc9218e0acb091': 'foo bar'
}

console.log(value['0d1df598a5539ab5b6b410b339dc9218e0acb091']);