János
János

Reputation: 35090

How can I get the first key from JSON in javascript / NODE.js?

I have this JSON:

{ "%7B%22userName%22%3A%22JAK%22%7D": "" }

How can I retrieve the first key?

Upvotes: 1

Views: 1557

Answers (2)

Simon
Simon

Reputation: 5247

var key = Object.keys({ '%7B%22userName%22%3A%22JAK%22%7D': '' })[0];

Upvotes: 6

Thomas Urban
Thomas Urban

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

Related Questions