-5

Folks, DynamoDB call returns a JSON Object, which I would like to parse, and grab the password hash field

jsonString = JSON.stringify(data)
console.log(jsonString)

output:

{"Count":1,"Items":[{"token":{"S":"token"},"uid":{"S":"33c02130-66b5-11e3-bdb0-7d9889f293b5"},"password":{"S":"$2a$10$ervzJ.DWjHOXRtJSugTaWuquI2OvPLyipa4YXecc/2KdQnmhrHxr6"},"username":{"S":"foo"},"plate":{"S":"dinner"},"name":{"S":"Test Name"},"server":{"S":"bar"}}]}

How would i parse this string, and retrieve the 'password' field? The following code does not work:

console.log(jsonString.password)
console.log(jsonString.uid)

The following returns undefined:

console.log(data.password);

Thanks!

Cmag
  • 14,946
  • 25
  • 89
  • 140

1 Answers1

1

This is already an object, so you can do this:

var str = {"Count":1,"Items":[{"token":{"S":"token"},"uid":{"S":"33c02130-66b5-11e3-bdb0-7d9889f293b5"},"password":{"S":"$2a$10$ervzJ.DWjHOXRtJSugTaWuquI2OvPLyipa4YXecc/2KdQnmhrHxr6"},"username":{"S":"foo"},"plate":{"S":"dinner"},"name":{"S":"Test Name"},"server":{"S":"bar"}}]};

alert(str.Items[0].password.S);
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Ringo
  • 3,795
  • 3
  • 22
  • 37
  • 1
    Not quite. While your example works, it misrepresents what actually is happening here. His `data`, like your `str` is not a string, but an object. His `data` object should not be `stringify`'d as he posted, but just used as it is. Like this: `data.Items[0].password.S`. – Jonathan M Dec 17 '13 at 01:28