最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSON.parse Dynamodb - Stack Overflow

programmeradmin2浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 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

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.

发布评论

评论列表(0)

  1. 暂无评论