json - 通过变量将 JSON 传递给 YAML

标签 json yaml aws-cloudformation

我有一个用于创建 EventBridge 规则的 YAML 文件。该规则调用一个步骤函数,该函数接受 JSON 字符串作为输入。当我使用变量传递字符串时,AWS CloudFormation 报告错误,这似乎表明引号已从 JSON 中删除。

如何将 JSON 字符串传递到 YAML 中,以便 AWS 获取正确的 YAML?

JSON:

{"Guid1": "d8a47ae8-50c5-4d76-937f-124d353e125f"}

相关 YAML:

Parameters:
  StepFunctionInput:
    Description: The JSON input to use when triggering the step function.
    Type: String

  ScheduledRule:
    Type: AWS::Events::Rule
    Properties:
      RoleArn: !GetAtt Role.Arn
      ScheduleExpression: !Sub cron(0/${StartupIntervalMinutes} * * * ? *)
      State: ENABLED
      Targets:
        - Arn: !Ref StepFunctionArn
          Id: StepFunctionV1
          Input: !Ref StepFunctionInput

AWS 错误:

JSON syntax error in input for target StepFunctionV1: [Source: (String)"{Guid1: d8a47ae8-50c5-4d76-937f-124d353e125f}"; line: 1, column: 3] (Service: AmazonCloudWatchEvents; Status Code: 400; Error Code: ValidationException; Request ID: 71520ea5-d959-495a-b89f-dbc812f78e59; Proxy: null)

将 JSON 直接放入 YAML(而不是通过变量传递)可以:

          Input: '{"Guid1": "d8a47ae8-50c5-4d76-937f-124d353e125f"}'

最佳答案

事实证明这是一个 PowerShell 问题。 PowerShell 在将字符串传递到 AWS CLI 之前删除了该字符串中的双引号。为了解决这个问题,我用\转义了每个字符串 AWS documentation .

Before PowerShell sends a command to the AWS CLI, it determines if your command is interpreted using typical PowerShell or CommandLineToArgvW quoting rules. When PowerShell processes using CommandLineToArgvW, you must escape characters with a backslash \

Double quotation marks " " are called expandable strings. Variables can be passed in expandable strings. For double quoted strings you have to escape twice using ``` for each quote instead of only using a backtick. The backtick escapes the backslash, and then the backslash is used as an escape character for the CommandLineToArgvW process.

关于json - 通过变量将 JSON 传递给 YAML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73708745/

相关文章:

powershell - Azure Devops yaml 构建定义 PowerShell 内联语法问题

amazon-web-services - RDS 代理目标组不可用

javascript - 如何从 JSON 对象获取值

docker - 在 docker-compose.yml 中使用继承

php - YAML解析错误-多维数组

amazon-web-services - AWS CDK : Adding new stack not working (not showing up)

amazon-web-services - 无法查看 Cloudformation 模板

arrays - 在逻辑应用程序中处理复杂 JSON 数据的正确/惯用方法是什么?

java - 无法从 POJO 生成 Json 架构

javascript - 我无法将字符串转换为 JavaScript 对象