AWS DynamoDB与PutItem的问题。什么是正确的JSON格式?

时间:2017-07-27 15:34:13

标签: json amazon-web-services amazon-dynamodb validationerror

我在将PutItem请求发送到DynamoDB时遇到问题。我理解类似的问题已被回答here,但似乎我这里没有空值。

我的JSON是:

{
  "@type": "MessageCard",
  "@context": "http://schema.org/extensions",
  "summary": "Hero card testing",
  "title": null,
  "text": null,
  "themeColor": "19b5fe",
  "sections": [
    {
      "title": null,
      "activityImage": "https://pbs.twimg.com/profile_images/862653089916096512/ljJwcmFp_bigger.jpg",
      "activityTitle": "Hero image card",
      "activitySubtitle": "This is a test",
      "facts": [],
      "text": null,
      "heroImage": {
        "image": "https://pbs.twimg.com/media/DFv74A0XkAEdwQ_.jpg"
      },
      "images": []
    }
  ]
}

错误是ValidationException:

{
    "TableName":"Unity",
    "Item":{
        "id":{
            "S":"73709359-ac78-46a0-8ca6-414393e33339"
        },
        "Session":{
            "S":"b6ba8b6d-ce27-4585-aee5-b9a2393e54da"
        },
        "Pos":{
            "X":{
                "S":"-16.8"
            },
            "Y":{
                "S":"-4.492812"
            }
        },
        "Time":{
            "S":"7/27/2017 3:21:25 PM"
        }
    }
}

任何人都知道发生了什么?

1 个答案:

答案 0 :(得分:1)

请尝试以下代码。我为属性Pos添加了数据类型地图

var dynamoDB = new AWS.DynamoDB;

var params = {
    TableName: "Unity",
    Item: {
        "id": {
            S: "73709359-ac78-46a0-8ca6-414393e33339"
        },
        "Session": {
            S: "b6ba8b6d-ce27-4585-aee5-b9a2393e54da"
        },
        "Pos": {
            M: {
                "X": {
                    S: "-16.8"
                },
                "Y": {
                    S: "-4.492812"
                }
            }
        },
        "Time": {
            S: "7/27/2017 3:21:25 PM"
        }
    }
};

console.log("Adding a new item...");
dynamoDB.putItem(params, function (err, data) {
    if (err) {
        console.error("Unable to add item. Error JSON:", JSON.stringify(err,
            null, 2));
    } else {
        console.log("Added item:", JSON.stringify(data, null, 2));
    }
});