3

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?

6
  • Show us the expected output! Commented Apr 21, 2016 at 4:37
  • show the expected way and output that you want ? if you want to parse just like search by the keys ,you can use loadash before search the object .. Commented Apr 21, 2016 at 4:40
  • try to use underscore functions, underscorejs.org/#pluck Commented Apr 21, 2016 at 4:42
  • The post claims to have a [JavaScript] object. JSON.parse takes JSON text. A useful start would be to omit the incorrect JSON.parse usage - edit the post to remove the incorrect information. Commented Apr 21, 2016 at 4:42
  • Are you sure the objects in array is supposed to have different keys? If those values are of same type, you should use same key name and it'll be much simpler. Commented Apr 21, 2016 at 4:47

6 Answers 6

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

"String to JSON" - no, it's for converting a JSON string (A string that adhere's to JavaScript Object Notation) to JavaScript object. JavaScript Object != JSON
@Rajeshwar You're welcome, so please mark question as solved, T J you are right !
1

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
    }  
});

Comments

0

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>

Comments

0

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>

Comments

0


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

Comments

-1

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);
}

1 Comment

Edit: Updated to answer question without changing data structure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.