Rajeshwar
Rajeshwar

Reputation: 381

parse json object in javascript to get key and values

I have a object, need to parse the below data

  var data= [{"obj1":"2122"},{"obj2":"123"}]

to get both the keys and values in javascript. I yried to use:

var obj = JSON.parse(data);
for(var prop in data) {
if(data.hasOwnProperty(prop))
  console.log(prop);
}

The values that are obtained in console are

Object {obj1: "2122"}
Object {obj2: "123"}

But I need to access the values seperately and not as object. How to retrieve it from that object?

Upvotes: 3

Views: 20007

Answers (6)

Amaldev ps
Amaldev ps

Reputation: 265

Here you will get the values in array "values".

var data= [{"obj1":"2122"},{"obj2":"123"}]
 
data = JSON.stringify(data);
 
var values = [];
 
JSON.parse(data, function (key, value) {

    if (typeof(value) != "object") {
        values.push({[key]:value});
	// values.push(value); //if you need a value array
    }  
});

Upvotes: 1

nisar
nisar

Reputation: 1065


Try this one..

var obj = JSON.parse('[{"obj1":2122},{"obj2":123}]');
obj.forEach(function(ElementObject){
var keys=Object.keys(ElementObject);
 console.log(keys[0],ElementObject[Object.keys(ElementObject)]); 
}
);

JsFiddle

Upvotes: 0

Alexander Higgins
Alexander Higgins

Reputation: 6923

First:

var data = [{"obj1":"2122"},{"obj2":"123"}]

This line will create an Array. No need for:

var obj = JSON.parse(data);

If you want to access via key/value you need to restructure your data.

var data = [{key:"obj1",value:"2122"},{key:"obj2", value:"123"}];
for( var index in data)
{ 
  var item = data[index]; 
  console.log(item.key);
  console.log(item.value);

}

Alternately you can map:

 var keys = [{
  "obj1": "2122"
}, {
  "obj2": "123"
}];
var op = data.map(function(item) {
  var keys = Object.keys(item);
  var arr = [];
  keys.forEach(function(key) {
    console.log(key);
    arr.push(key);
  });
  return arr;
});
var i=0;
for(var i=0;i<op.length;i++)
{
  console.log(i);
  var pair=data[i];
  console.log(pair);
  var key=op[i][0];
  console.log(key);
  var value=pair[key];
  console.log(value);
}

Upvotes: -1

Manjeet Barnala
Manjeet Barnala

Reputation: 2995

Try this code use $.each function to parse object..

 var data= [{"obj1":"2122"},{"obj2":"123"}]
 $.each(data, function(key, val) {
 	 $.each(val, function(k, v) {
		 console.log('key ='+k);
		 console.log('value ='+v);
		 alert('key = '+k+', value = '+v);
	 });
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 0

mimiz
mimiz

Reputation: 2288

JSON.parse is use to parse JSONString to Javascript Object.

You can not use it directly on a JavaScript Object ...

Anyway, your object is an array so you may do :

var arr = JSON.parse(data);
arr.forEach(function(elementObject){
    var keys = Object.keys(elementObject);
    keys.forEach(function(key){
      console.log(key + ":"+elementObject[key]);
    })
});

Cheers

Upvotes: 5

Rayon
Rayon

Reputation: 36609

Use Array#map and extract keys of the object in the callback. Iterate them to gain the value of each key using Array#forEach

var data = [{
  "obj1": "2122"
}, {
  "obj2": "123"
}];
var op = data.map(function(item) {
  var keys = Object.keys(item);
  var arr = [];
  keys.forEach(function(key) {
    arr.push(item[key]);
  });
  return arr;
});
console.log(op);
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

Upvotes: 0

Related Questions