【问题标题】:Apache VTL - Copy nodeApache VTL - 复制节点
【发布时间】:2021-06-19 12:27:52
【问题描述】:

有没有办法使用 apache VTL 进行深度复制? 我试图通过requestTemplates 使用x-amazon-apigateway-integration。 输入的JSON如下图,

{
  "userid": "21d6523137f6",
  "time": "2020-06-16T15:22:33Z",
  "item": {
    "UserID" : { "S": "21d6523137f6" },
    <... some complex json nodes here ...>,
    "TimeUTC" : { "S": "2020-06-16T15:22:33Z" },
  }
} 

requestTemplate如下图,

        requestTemplates:
          application/json: !Sub
            - |
              #set($inputRoot = $input.path('$'))
              {
                "TableName": "${tableName}",
                "ConditionExpression": "attribute_not_exists(TimeUTC) OR TimeUTC > :sk",
                "ExpressionAttributeValues": {
                  ":sk":{
                    "S": "$util.escapeJavaScript($input.path('$.time'))"
                  }
                },
                "Item": "$input.path('$.item')", <== Copy the entire item over to Item.
                "ReturnValues": "ALL_OLD",
                "ReturnConsumedCapacity": "INDEXES",
                "ReturnItemCollectionMetrics": "SIZE"
              }
            - {
            tableName: !Ref EventsTable
            }

问题是,项目被复制了,

  "Item": "{UserID={S=21d6523137f6}, Lat={S=37.33180957}, Lng={S=-122.03053391}, ... other json elements..., TimeUTC={S=2020-06-16T15:22:33Z}}",

如你所见,整个嵌套的 json 变成了一个单一的属性。虽然我预计它会像下面这样自行成为一个成熟的 json 节点,

  "Item": {
    "UserID" : { "S": "21d6523137f6" },
    "Lat": { "S": "37.33180957" },
    "Lng": { "S": "-122.03053391" },
    <.... JSON nodes ...>
    "TimeUTC" : { "S": "2020-06-20T15:22:33Z" }
  },

是否可以像上面那样在 json 节点上进行深度/嵌套复制操作,而无需执行迭代节点并附加子节点或 json 节点变量等的功夫...

顺便说一句,我使用的是 AWS API Gateway 请求模板,因此它可能不支持所有 Apache VTL 模板选项。

【问题讨论】:

    标签: json amazon-web-services aws-api-gateway vtl


    【解决方案1】:

    您需要使用$input.json 方法而不是$input.path

    "Item": $input.json('$.item'),
    

    请注意,我删除了双引号。

    如果你有双引号是因为你想对$.item 进行字符串化,你可以这样做:

    "Item": "$util.escapeJavaScript($input.json('$.item'))",
    

    【讨论】:

      猜你喜欢
      • 2011-08-28
      • 1970-01-01
      • 2017-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      相关资源
      最近更新 更多