0

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

var user = [ { _id: 5902086ecbc0dd11e4870fd9,
 password: '2ドルa08ドル$FIpkmFT1WDZggQYyBA4CVuop6pelbKBfUEJ1/KAVIV2Si9Ho1EYhi',
 email: '[email protected]',
 lastName: 'v',
 firstName: 'j',
 updatedDate: 2017年04月27日T15:04:14.483Z,
 createdDate: 2017年04月27日T15: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
12.2k7 gold badges63 silver badges133 bronze badges
asked Apr 27, 2017 at 15:55
6
  • 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 ...). Commented Apr 27, 2017 at 15:56
  • 1
    This is not JSON. There's nothing to parse! Commented Apr 27, 2017 at 15:56
  • Possible duplicate of What's the difference between Javascript Object and JSON object Commented Apr 27, 2017 at 15:58
  • Possible duplicate of Access / process (nested) objects, arrays or JSON Commented Apr 27, 2017 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 Commented Apr 27, 2017 at 15:58

5 Answers 5

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);
answered Apr 27, 2017 at 15:58
Sign up to request clarification or add additional context in comments.

4 Comments

user is already an array of objects not JSON. JSON is a string representation of objects.
yes it solved my problem. sorry for noob question question. Im newby for nodejs. thanks alot
I fixed my answer to clarify that change @ibrahimmahrir
@PeterLaBanca a response after 4 years, 1 month and 6 days haha. Better late than never :-)
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.

answered Apr 27, 2017 at 15:59

Comments

2

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

DEMO

var user = [{ _id: "5902086ecbc0dd11e4870fd9",
 password: '2ドルa08ドル$FIpkmFT1WDZggQYyBA4CVuop6pelbKBfUEJ1/KAVIV2Si9Ho1EYhi',
 email: '[email protected]',
 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);

answered Apr 28, 2017 at 8:26

Comments

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);
answered Apr 27, 2017 at 16:11

Comments

1

Try This:

var user = [ {_id:'5902086ecbc0dd11e4870fd9',password: '2ドルa08ドル$FIpkmFT1WDZggQYyBA4CVuop6pelbKBfUEJ1/KAVIV2Si9Ho1EYhi',email: '[email protected]',lastName: 'v',firstName: 'j',updatedDate: '2017-04-27T15:04:14.483Z',createdDate:' 2017年04月27日T15:04:14.483Z' } ];
var obj = user[0];
console.log(obj.password);
answered Apr 27, 2017 at 16:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.