Reputation: 35090
I have this JSON:
{ "%7B%22userName%22%3A%22JAK%22%7D": "" }
How can I retrieve the first key?
Upvotes: 1
Views: 1557
Reputation: 5247
var key = Object.keys({ '%7B%22userName%22%3A%22JAK%22%7D': '' })[0];
Upvotes: 6
Reputation: 5071
Assuming your JSON is stored in variable data
:
Object.keys( data ).shift()
Another approach might be this:
var name; for ( name in data ) { if ( data.hasOwnProperty( name ) ) break; }
This second approach isn't creating separate array just to get its first element.
Upvotes: 1