Search code examples
amazon-web-servicesaws-step-functions

Retrieve the value within a "foo.bar" string in an AWS Lambda payload


In an AWS Step Function, I have a Lambda function whose output looks like this:

{
"foo.bar": "Something"
}

In the next Lambda function, I need to pass this value through the payload.

{
"value.$": "$.foo.bar"
}

When I do this, the step doesn't find the value. I think it might be because it expects something like this:

{
"foo": "bar"
}

I didn't really see a solution in the AWS documentation. When looking for help from AI, I found things like:

{
"value.$": "$.foo.bar.$"
}

... And this didn't work.


Solution

  • When you have a period in your key name like that, you need to use the bracket notation as in the example below. Otherwise, the JSONPath interpreter thinks you are looking for something like:

    {
      "foo": {
        "bar": "Something"         
      }
    } 
    

    In this example, the first choice state uses the dot notation as you had and doesn't find the key. The second one uses bracket notation and does.

    {
      "StartAt": "Simulate Lambda Function",
      "States": {
        "Simulate Lambda Function": {
          "Type": "Pass",
          "Result": {
            "foo.bar": "somevalue"
          },
          "Next": "Try to find using dot notation"
        },
        "Try to find using dot notation": {
          "Type": "Choice",
          "Choices": [
            {
              "Variable": "$.foo.bar",
              "IsPresent": true,
              "Next": "Found It!"
            }
          ],
          "Default": "Try to find using bracket notation"
        },
        "Try to find using bracket notation": {
          "Type": "Choice",
          "Choices": [
            {
              "Variable": "$['foo.bar']",
              "IsPresent": true,
              "Next": "Found It!"
            }
          ],
          "Default": "Didn't find it. :-("
        },
        "Found It!": {
          "Type": "Succeed"
        },
        "Didn't find it. :-(": {
          "Type": "Fail"
        }
      }
    }
    

    enter image description here