0

eg if variable jsonstring contains

{"prod_name":"GM","quantity":100,"price":54.5,"type":"Limit"}

for a code like

   var obj= JSON.parse(jsonstring);

Without knowing the string content is there a way to extract the property/ value names?

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
Aditya
  • 260
  • 5
  • 13

2 Answers2

2

You can loop through the object properties.

for ( var prop in obj ) {
    if ( obj.hasOwnProperty(prop) ) {
        console.log( prop + ': ' + obj[prop] );
    }
}
pmandell
  • 4,288
  • 17
  • 21
1

In JavaScript 1.8.5, Object.getOwnPropertyNames returns an array of all properties found directly upon a given object.

Object.getOwnPropertyNames ( obj )
Aditya
  • 4,453
  • 3
  • 28
  • 39