Folks, Im getting back the following JSON object, which I would like to parse:
{"Count":1,"Items":[{"foo":{"S":"bar"}}]}
If I do the following, I get the 'bar' of the foo item?:
foo = JSON.stringify(result.Items)
foo = JSON.parse(foo)
console.log(foo)
fails if i try:
console.log(foo.bar)
Thanks!
Folks, Im getting back the following JSON object, which I would like to parse:
{"Count":1,"Items":[{"foo":{"S":"bar"}}]}
If I do the following, I get the 'bar' of the foo item?:
foo = JSON.stringify(result.Items)
foo = JSON.parse(foo)
console.log(foo)
fails if i try:
console.log(foo.bar)
Thanks!
Share Improve this question asked Dec 3, 2013 at 0:14 CmagCmag 15.8k25 gold badges97 silver badges146 bronze badges2 Answers
Reset to default 6In your example you would actually need:
console.log(foo[0].foo.S); // "bar"
But I would suggest using dynamodb-marshaler
A. Because I wrote it.
B. Because it translates DynamoDb attribute values for you so you don't need to deal with "S" type keys.
var item = unmarshalItem(result.Items[0]);
console.log(item.foo); // "bar"
or even more slick when you have multiple items returned:
var items = result.Items.map(unmarshalItem);
items.each(function(item) { console.log(item.foo); }); // "bar"
EDIT:
There is now a DocumentClient which does this marshal work as part of the aws-sdk-js
That's because there's no "bar" property of result.Items
— it's an array.
Try
console.log(foo[0].foo.bar);
Or else, when you stringify it in the first place:
var foo = JSON.stringify(result.Items[0].foo);
and then
console.log(foo.bar);
should work.