0

I have this JSON array, and what I want is to get the password field alone

var user = [ { _id: 5902086ecbc0dd11e4870fd9,
    password: '$2a$08$FIpkmFT1WDZggQYyBA4CVuop6pelbKBfUEJ1/KAVIV2Si9Ho1EYhi',
    email: 'jv100@gmail.com',
    lastName: 'v',
    firstName: 'j',
    updatedDate: 2017-04-27T15:04:14.483Z,
    createdDate: 2017-04-27T15:04:14.483Z } ]

I tried to parse it using this code

var obj = JSON.parse(user);
console.log(user.password);

but still it is undefined.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
JerVi
  • 131
  • 3
  • 14
  • It doesn't need parsing. It's already an array. And since it's an array of objects you'll have to use it like this: `user[ index ].password;` (`user[0].password;` for the first object ...). – ibrahim mahrir Apr 27 '17 at 15:56
  • 1
    This is not JSON. There's nothing to parse! – Álvaro González Apr 27 '17 at 15:56
  • Possible duplicate of [What's the difference between Javascript Object and JSON object](http://stackoverflow.com/questions/6489783/whats-the-difference-between-javascript-object-and-json-object) – Álvaro González Apr 27 '17 at 15:58
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Heretic Monkey Apr 27 '17 at 15:58
  • actually that is what I received from my db (I'm using moongose for that), and I dont know how will I get the password field. thanks for the response, it helps – JerVi Apr 27 '17 at 15:58
  • Then I don't think you've posted real code. If you are getting data from a database you cannot have a fragment of JavaScript code with an array literal, unless of course your database wrapper decodes it for you. In either case, it's unlikely we can offer help. – Álvaro González Apr 27 '17 at 16:27

5 Answers5

4

User is already a list of objects, so you don't need to parse it. However, it is an array. So if you meant for it to be an array, you'd need to access the password by using this code:

console.log(user[0].password);
Peter LaBanca
  • 475
  • 3
  • 9
2

It's already an array there is nothing to parse. You can access your property via :

console.log(user[0].password);

You can't access your property with user.password because user variable is not object, it's an array, your object is stored at the zero index of your array.

aprogrammer
  • 1,764
  • 1
  • 10
  • 20
2

You already have JSON object. hence, no need to parse it again.

DEMO

var user = [{ _id: "5902086ecbc0dd11e4870fd9",
    password: '$2a$08$FIpkmFT1WDZggQYyBA4CVuop6pelbKBfUEJ1/KAVIV2Si9Ho1EYhi',
    email: 'jv100@gmail.com',
    lastName: 'v',
    firstName: 'j',
    updatedDate: "2017-04-27T15:04:14.483Z",
    createdDate: "2017-04-27T15:04:14.483Z" } ];
    
var password = user[0].password;

console.log(password);
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
1

The variable 'user' is not a JSON array. It's an array with a single Javascript object as its element. JSON.parse(arg) can only be used to parse a JSON string to a plain Javascript object. That being said, to access the javascript object within the array, you can do:

var userData = user[0];

To access the password within the variable, userData, you can do:

var password = userData.password;

Log the password to the console with:

console.log(password);
Iyanu Adelekan
  • 396
  • 2
  • 7
1

Try This:

var user = [ {_id:'5902086ecbc0dd11e4870fd9',password: '$2a$08$FIpkmFT1WDZggQYyBA4CVuop6pelbKBfUEJ1/KAVIV2Si9Ho1EYhi',email: 'jv100@gmail.com',lastName: 'v',firstName: 'j',updatedDate: '2017-04-27T15:04:14.483Z',createdDate:' 2017-04-27T15:04:14.483Z' } ];

var obj = user[0];
console.log(obj.password);