DEV Community

Mikita M
Mikita M

Posted on

2

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' %}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

and to check if processedByExecutor is false.

$parse($states.input[0].body).processedByExecutor = false
Enter fullscreen mode Exit fullscreen mode

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) %}
Enter fullscreen mode Exit fullscreen mode

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. πŸ˜‰

Happy cloud computing! πŸŒ₯οΈπŸš€
And feel free to comment and to connect with me on LinkedIn!

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

πŸ‘‹ Kindness is contagious

If this article connected with you, consider tapping ❀️ or leaving a brief comment to share your thoughts!

Okay