操作JSON输入以生成JSON项目列表

huangapple go评论57阅读模式
英文:

Manipulate JSON input to produce a JSON list of items

问题

我正在尝试找到一种方法来操作我的输入JSON,以便我可以将其传递给一个调用另一个步骤函数以进行处理的映射函数。

我的输入JSON看起来像这样:

{
  "notificationType": "Received",
  "mail": {
    "timestamp": "2023-05-31",
    "source": "source@gmail.com",
    "messageId": "xyz",
    "destination": [
      "email1",
      "email2"
    ],
    "headersTruncated": false,
    "headers": [...],
    "commonHeaders": {
      // 其他内容
    }
  }
}

我想要生成一个类似这样的JSON:

[{
  "email_address": "email1",
  "notificationType": "Received",
  "mail": {
    "timestamp": "2023-05-31T16:24:48.413Z",
    "source": "source@gmail.com",
    "messageId": "xyz",
    "destination": [
      "email1",
      "email2"
    ],
    "headersTruncated": false,
    "headers": [...],
    "commonHeaders": {
      // 其他内容
    }
},
{
  "email_address": "email2",
  "notificationType": "Received",
  "mail": {
    "timestamp": "2023-05-31T16:24:48.413Z",
    "source": "source@gmail.com",
    "messageId": "xyz",
    "destination": [
      "email1",
      "email2"
    ],
    "headersTruncated": false,
    "headers": [...],
    "commonHeaders": {
      // 其他内容
    }
}]

这个列表将传递给一个映射步骤,该步骤将执行另一个步骤函数。或者,如果有一种方法可以将原始输入与$.mail.destination列表一起传递给映射,那也可以,但我找不到这样做的方法。

在AWS的内置函数中,我没有找到以这种方式操作JSON的方法。

英文:

I am trying to find a way to manipulate my input JSON so that I can pass it to a map function which would call another step function for processing.

My input JSON looks like this:

  "notificationType": "Received",
  "mail": {
    "timestamp": "2023-05-31",
    "source": "source@gmail.com",
    "messageId": "xyz",
    "destination": [
      "email1",
      "email2"
    ],
    "headersTruncated": false,
    "headers": [...],
    "commonHeaders": {
      ....
    }
}

I would like to produce a JSON that would look like this:

[{
  "email_address": "email1"
  "notificationType": "Received",
  "mail": {
    "timestamp": "2023-05-31T16:24:48.413Z",
    "source": "source@gmail.com",
    "messageId": "xyz",
    "destination": [
      "email1",
      "email2"
    ],
    "headersTruncated": false,
    "headers": [...],
    "commonHeaders": {
      ....
    }
},
{
  "email_address": "email2"
  "notificationType": "Received",
  "mail": {
    "timestamp": "2023-05-31T16:24:48.413Z",
    "source": "source@gmail.com",
    "messageId": "xyz",
    "destination": [
      "email1",
      "email2"
    ],
    "headersTruncated": false,
    "headers": [...],
    "commonHeaders": {
      ....
    }
}]

This list would be passed to a map step which would execute another step function.
Or if there is any way to pass original input to a Map along with $.mail.destination list that would also work, but I couldn't find a way to do that either.

Couldn't find anything in AWS built in functions to manipulate json in such a way

答案1

得分: 1

如果我理解正确,您的输入是一个单个JSON对象。该对象包含名为destination的项目数组。您想要使用这个数组来创建一个项目数组,每个项目都包括原始对象中的一些数据作为上下文。

如果是这样,我会使用一个Map状态来完成这个任务。请参考下面的Amazon状态语言(ASL)示例。它使用了一个Map状态,将您的destination数组设置为ItemsPath,这意味着每个destination都会有一次迭代。它还使用ItemSelector来塑造每个迭代的输入,以包含来自原始输入的公共数据。然后,它使用Pass State中的Parameters来按照您的指定方式塑造项目。

{
  "StartAt": "Generate Input",
  "States": {
    "Generate Input": {
      "Type": "Pass",
      "Result": {
        "notificationType": "Received",
        "mail": {
          "timestamp": "2023-05-31",
          "source": "source@gmail.com",
          "messageId": "xyz",
          "destination": [
            "email1",
            "email2"
          ],
          "headersTruncated": false,
          "headers": [
            "header1",
            "header2",
            "header3"
          ],
          "commonHeaders": {
            "commonHeader1": "value1",
            "commonHeader2": "value2",
            "commonHeader3": "value3"
          }
        }
      },
      "Next": "Map"
    },
    "Map": {
      "Type": "Map",
      "ItemProcessor": {
        "ProcessorConfig": {
          "Mode": "INLINE"
        },
        "StartAt": "Shape Item",
        "States": {
          "Shape Item": {
            "Type": "Pass",
            "End": true,
            "Parameters": {
              "email_address.$": "$.itemValue",
              "notificationType.$": "$.originalInput.notificationType",
              "mail.$": "$.originalInput.mail"
            }
          }
        }
      },
      "End": true,
      "ItemsPath": "$.mail.destination",
      "ItemSelector": {
        "originalInput.$": "$",
        "itemValue.$": "$$.Map.Item.Value"
      }
    }
  }
}

然后,这将产生以下结果,我认为这是您想要的。然后,您可以将这个结果传递给另一个Map状态。或者这可能允许您在上面定义的Pass状态之后添加处理逻辑。

[
  {
    "email_address": "email1",
    "mail": {
      "timestamp": "2023-05-31",
      "source": "source@gmail.com",
      "messageId": "xyz",
      "destination": [
        "email1",
        "email2"
      ],
      "headersTruncated": false,
      "headers": [
        "header1",
        "header2",
        "header3"
      ],
      "commonHeaders": {
        "commonHeader1": "value1",
        "commonHeader2": "value2",
        "commonHeader3": "value3"
      }
    },
    "notificationType": "Received"
  },
  {
    "email_address": "email2",
    "mail": {
      "timestamp": "2023-05-31",
      "source": "source@gmail.com",
      "messageId": "xyz",
      "destination": [
        "email1",
        "email2"
      ],
      "headersTruncated": false,
      "headers": [
        "header1",
        "header2",
        "header3"
      ],
      "commonHeaders": {
        "commonHeader1": "value1",
        "commonHeader2": "value2",
        "commonHeader3": "value3"
      }
    },
    "notificationType": "Received"
  }
]
英文:

If I understand correctly, you have an single JSON object as input. That object contains an array of items called destination. You want to use this to create an array of items, one for each destination, that also includes some of the data from original object as context.

If so, I'd use a Map state to do this. See the Amazon State Language (ASL) below to illustrate. This uses an Map state with your destination array as the ItemsPath which means you will have one iteration per destination. It also uses ItemSelector to shape the input to each of the iterations to include common data from the original input. It then uses Parameters in a Pass State to shape the item as you specified.

{
  "StartAt": "Generate Input",
  "States": {
    "Generate Input": {
      "Type": "Pass",
      "Result": {
        "notificationType": "Received",
        "mail": {
          "timestamp": "2023-05-31",
          "source": "source@gmail.com",
          "messageId": "xyz",
          "destination": [
            "email1",
            "email2"
          ],
          "headersTruncated": false,
          "headers": [
            "header1",
            "header2",
            "header3"
          ],
          "commonHeaders": {
            "commonHeader1": "value1",
            "commonHeader2": "value2",
            "commonHeader3": "value3"
          }
        }
      },
      "Next": "Map"
    },
    "Map": {
      "Type": "Map",
      "ItemProcessor": {
        "ProcessorConfig": {
          "Mode": "INLINE"
        },
        "StartAt": "Shape Item",
        "States": {
          "Shape Item": {
            "Type": "Pass",
            "End": true,
            "Parameters": {
              "email_address.$": "$.itemValue",
              "notificationType.$": "$.originalInput.notificationType",
              "mail.$": "$.originalInput.mail"
            }
          }
        }
      },
      "End": true,
      "ItemsPath": "$.mail.destination",
      "ItemSelector": {
        "originalInput.$": "$",
        "itemValue.$": "$$.Map.Item.Value"
      }
    }
  }
}

This will then yield the following result, which I think is what you wanted. You could then pass this into another Map state. Or this might allow you to just add your processing logic after the Pass state in the ItemProcessor defined above.

[
  {
    "email_address": "email1",
    "mail": {
      "timestamp": "2023-05-31",
      "source": "source@gmail.com",
      "messageId": "xyz",
      "destination": [
        "email1",
        "email2"
      ],
      "headersTruncated": false,
      "headers": [
        "header1",
        "header2",
        "header3"
      ],
      "commonHeaders": {
        "commonHeader1": "value1",
        "commonHeader2": "value2",
        "commonHeader3": "value3"
      }
    },
    "notificationType": "Received"
  },
  {
    "email_address": "email2",
    "mail": {
      "timestamp": "2023-05-31",
      "source": "source@gmail.com",
      "messageId": "xyz",
      "destination": [
        "email1",
        "email2"
      ],
      "headersTruncated": false,
      "headers": [
        "header1",
        "header2",
        "header3"
      ],
      "commonHeaders": {
        "commonHeader1": "value1",
        "commonHeader2": "value2",
        "commonHeader3": "value3"
      }
    },
    "notificationType": "Received"
  }
]

huangapple
  • 本文由 发表于 2023年6月1日 12:05:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定