代码之家  ›  专栏  ›  技术社区  ›  Rilcon42

用JSON编写DynamoDB Map对象

  •  0
  • Rilcon42  · 技术社区  · 4 年前

    我试图使用JSON文档向表中添加值,但总是遇到错误。我的JSON规范有什么问题?

    • 主题是一个字符串(键)
    • 问题是问题/答案的映射

    我不断得到:

    错误:

    当我奔跑时 aws dynamodb batch-write-item --request-items file://notesTable.json :

    Parameter validation failed:
    Invalid type for parameter RequestItems.notesTable[0].PutRequest.Item.questions.
    M, value: [OrderedDict([('what is Dynamo?', OrderedDict([('S', 'a non-relational
     document based NoSQL database')]))])], type: <class 'list'>, valid types: <clas
    s 'dict'>
    

    目标:

    topics    questions
    ------    ---------
    Dynamo    {"what is Dynamo?":{"S":"a non-relational document based NoSQL database"}}
    

    JSON结构

    {
        "notesTable": [
            {
                "PutRequest": {
                    "Item": {
                        "topic":{"S":"Dynamo"},
                        "questions": {
                            "M": [
                                {"what is Dynamo?":{"S":"a non-relational document based NoSQL database"}}
                            ]
                        }
                    }
                }
            }
        ]
    }
    
    1 回复  |  直到 4 年前
        1
  •  1
  •   OARP    4 年前

    DynamoDB JSON格式不同于常规JSON格式。根据您的目标,格式化的json应该类似于以下内容:

    {
    "notesTable": [
        {
            "PutRequest": {
                "Item": {                    
                  "topic": {
                    "S": "Dynamo"
                  },
                  "questions": {
                    "L": [
                      {
                        "M": {
                          "what is Dynamo?": {
                            "S": "a non-relational document based NoSQL database"
                          }
                        }
                      }
                    ]
                  }
                }
            }
        }
      ]
    }
    

    有一些在线工具可以将常规json转换为dynamo-json。你可以试试这个: https://dynobase.dev/dynamodb-json-converter-tool/