3

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!

2 Answers 2

6

In 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

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

Comments

2

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.

Comments

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.