Search code examples
amazon-web-servicesaws-cdkaws-step-functionsaws-cdk-typescript

AWS CDK SFN JsonPath.stringAt to handle minus symbol


I have a function written in Typescript in my CDK code:

export function getS3FileLocation(dirName: string) {
  return sfn.JsonPath.stringAt(`$.myS3Bucket.${dirName}[0].location`);
}

Not surprise, here is the generated Cloudformation code if I pass my-dir as dirName:

$.myS3Bucket.my-dir[0].location

However, due to the minus symbol -, Cloudformation throws exception

The value for the field 'MyField.$' must be a valid JSONPath or a valid intrinsic function call at /States/My State/Parameters'

If I pass my_dir as dirName, the generated Cloudformation code would be $.myS3Bucket.my_dir[0].location, and Cloudformation does not throw exception.

How to handle the minus symbol -? For some reason, I have to pass my-dir as dirName.

The input payload is {"myS3Bucket":{"my-dir":[{"location":"location-1"}]}}


Solution

  • using keys as a dictionary instead of "." might work.

    $.myS3Bucket["my-dir"][0].location $["myS3Bucket"]["my-dir"][0].location