简体   繁体   中英

How can I use JSONPath to create a JSON object for use by AWS Step Functions?

Using an AWS step function, I'm attempting to select only certain data from the results of a task. For some unfathomable reason, AWS have chosen not to allow Query in step functions, so I'm using ResultsSelector . However, I'm struggling with the JSONPath that is required.

How can I use the ResultsSelector to construct my desired JSON object?

Take this result -

{
    "IsTruncated": false,
    "KeyMarker": "",
    "MaxKeys": 1000,
    "Name": "some-bucket-name",
    "Prefix": "some/prefix/",
    "VersionIdMarker": "",
    "Versions": [
        {
            "ETag": "\"02e9c20b7cd36fcf6e47926c26f0b39e\"",
            "IsLatest": true,
            "Key": "some/prefix/my.file",
            "LastModified": "2021-09-30T15:34:59Z",
            "Owner": {
                "Id": "1fd170056d1480a7c1c9b43f5bf0603d91cbabc4ec77eefdcaa10218c3a920f6"
            },
            "Size": 69606,
            "StorageClass": "STANDARD",
            "VersionId": "y6XzRsCUZcXMPHqwwnhAGLwTlmPoj9dj"
        },
        {
            "ETag": "\"01bc5b65afe6b0cc0722fc5da32a8a44\"",
            "IsLatest": false,
            "Key": "some/prefix/my.file",
            "LastModified": "2021-09-30T15:34:21Z",
            "Owner": {
                "Id": "1fd170056d1480a7c1c9b43f5bf0603d91cbabc4ec77eefdcaa10218c3a920f6"
            },
            "Size": 69407,
            "StorageClass": "STANDARD",
            "VersionId": "jPdKeUqnYlf0_eNXzHaYCvDfdHLOvRX7"
        }
    ]
}

What I'd like is to use the ResultsSelector to construct this JSON object -

{
    "Objects": [
        {
            "Key": "some/prefix/my.file",
            "VersionId": "y6XzRsCUZcXMPHqwwnhAGLwTlmPoj9dj"
        },
        {
            "Key": "some/prefix/my.file",
            "VersionId": "jPdKeUqnYlf0_eNXzHaYCvDfdHLOvRX7"
        }
    ]
}

However, the closest I've been able to get so far is by using this -

{
  "Key.$": "$.Versions[*].Key",
  "VersionId.$": "$.Versions[*].VersionId"
}

Which gets me this -

{
  "VersionId": [
    "y6XzRsCUZcXMPHqwwnhAGLwTlmPoj9dj",
    "jPdKeUqnYlf0_eNXzHaYCvDfdHLOvRX7",
  ],
  "Key": [
    "some/prefix/my.file",
    "some/prefix/my.file"
  ]
}

The Jayway JsonPath implementation gives the output with the Key. You need to verify if AWS Step function supports the below jsonpath .

Tool: https://jsonpath.herokuapp.com/

$.Versions[*].['Key','VersionId']

在此处输入图像描述

If it works then you can do something like

"Objects.$": "$.Versions[*].['Key','VersionId']",

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM