How to Parse JSON in AWS Step Function for State Machine Condition

AWS Step Functions are great, and they keep getting better. Not long ago, AWS introduced variables and JSONata data transformations for Step Functions, making them even more powerful. You can read more about it in the official AWS blog: Simplifying Developer Experience with Variables and JSONata. However, in this post, I want to focus on one specific use case: parsing a JSON string inside a State Machine condition to check certain object properties and decide the next step based on those values. The Use Case The input payload of your Step Function contains a property that is a string representation of a JSON object. You need to: Parse this property into a JSON object. Check the following conditions: deserializedObject.provider is either "aws" or "gcp". deserializedObject.processedByExecutor either doesn't exist or is false. If both conditions are met, the workflow should proceed with Step A; otherwise, it should go with Step B. Let's build this condition step by step. Step 1: Parsing JSON in Step Functions AWS Step Functions provide JSONata functions to transform data. The full list of functions is available here: AWS Step Functions JSONata Functions. For our case, the function we need is $parse(). In my case, the input to the State Machine comes from SQS, and the JSON string is stored in the body property. So, to extract and check the provider field, the JSONata expression looks like this: {% $parse($states.input[0].body).brand = 'aws' or $parse($states.input[0].body).brand = 'gcp' %} This ensures that the brand (or provider) is either AWS or GCP. Step 2: Checking processedByExecutor At first, I thought the condition would be easy: $parse($states.input[0].body).processedByExecutor != true However, this doesn't work if processedByExecutor is not defined—which was exactly the case for me. I even asked ChatGPT and Claude to generate the correct condition, but neither AI got it right. So, after some trial and error, I found the right way: To check if processedByExecutor does not exist $not($exists($parse($states.input[0].body).processedByExecutor)) and to check if processedByExecutor is false. $parse($states.input[0].body).processedByExecutor = false Final Condition Now, let's put everything together: {% ($parse($states.input[0].body).provider = 'aws' or $parse($states.input[0].body).provider = 'gcp') and ($not($exists($parse($states.input[0].body).processedByExecutor)) or $parse($states.input[0].body).processedByExecutor = false) %} You need to place this condition inside your AWS Step Function State Machine. Conclusion I hope this post helps if you ever run into a similar issue, especially if your AI assistant fails to generate the correct condition for you.

Mar 12, 2025 - 21:49
 0
How to Parse JSON in AWS Step Function for State Machine Condition

AWS Step Functions are great, and they keep getting better. Not long ago, AWS introduced variables and JSONata data transformations for Step Functions, making them even more powerful. You can read more about it in the official AWS blog: Simplifying Developer Experience with Variables and JSONata.

However, in this post, I want to focus on one specific use case: parsing a JSON string inside a State Machine condition to check certain object properties and decide the next step based on those values.

Software Engineer thinking about a proper JSONata expression

The Use Case

The input payload of your Step Function contains a property that is a string representation of a JSON object. You need to:

  1. Parse this property into a JSON object.
  2. Check the following conditions:
  • deserializedObject.provider is either "aws" or "gcp".
  • deserializedObject.processedByExecutor either doesn't exist or is false.

If both conditions are met, the workflow should proceed with Step A; otherwise, it should go with Step B.

Let's build this condition step by step.

Step 1: Parsing JSON in Step Functions

AWS Step Functions provide JSONata functions to transform data. The full list of functions is available here: AWS Step Functions JSONata Functions.

For our case, the function we need is $parse().

In my case, the input to the State Machine comes from SQS, and the JSON string is stored in the body property. So, to extract and check the provider field, the JSONata expression looks like this:

{% $parse($states.input[0].body).brand = 'aws'
or
$parse($states.input[0].body).brand = 'gcp' %}

This ensures that the brand (or provider) is either AWS or GCP.

Step 2: Checking processedByExecutor

At first, I thought the condition would be easy:

$parse($states.input[0].body).processedByExecutor != true

However, this doesn't work if processedByExecutor is not defined—which was exactly the case for me. I even asked ChatGPT and Claude to generate the correct condition, but neither AI got it right.

So, after some trial and error, I found the right way:

To check if processedByExecutor does not exist

$not($exists($parse($states.input[0].body).processedByExecutor))

and to check if processedByExecutor is false.

$parse($states.input[0].body).processedByExecutor = false

Final Condition

Now, let's put everything together:

{% ($parse($states.input[0].body).provider = 'aws'
or $parse($states.input[0].body).provider = 'gcp')
and ($not($exists($parse($states.input[0].body).processedByExecutor))
or $parse($states.input[0].body).processedByExecutor = false) %}

You need to place this condition inside your AWS Step Function State Machine.

AWS Setup

Conclusion

I hope this post helps if you ever run into a similar issue, especially if your AI assistant fails to generate the correct condition for you.