Skip to content

How can I serialize data to a JSON string in JSONata within step functions? (equivalent of States.JsonToString intrinsic function)

0

In trying to use JSONata in newer step function flows I am finding it lacks some functionality that was in JSONPath (while having more functionality in other areas).

Specifically there is a $parse to go from JSON String -> parsed JSON Object, but I cannot find the reverse.

In JSONPAth based step functions I had States.JsonToString that I could use to generate JSON serialized data, but I see no equivalent in JSONata (in either the AWS docs or the JSONata website docs)

asked 10 months ago273 views

1 Answer
0

In AWS Step Functions with JSONata, you can use the $string() function to serialize a JSON object into a string, which is the equivalent of the States.JsonToString intrinsic function used in JSONPath.

When using JSONata as your query language in Step Functions, you would format your expression like this:

"Output": "{% $string(yourObject) %}"

For example, if you have a JSON object that you want to convert to a string:

"Output": "{% $string({ \"foo\": \"bar\" }) %}"

This will produce a JSON string representation of the object.

It's worth noting that when you set QueryLanguage to "JSONata" at either the state machine or task level, you'll use the Arguments and Output fields instead of JSONPath fields like InputPath, ResultPath, ResultSelector, and OutputPath.

The JSONata expressions in Step Functions are enclosed in {% %} delimiters, and you have access to more functions and operators compared to JSONPath, though some functionality may differ between the two query languages.
Sources
Transforming data with JSONata in Step Functions - AWS Step Functions
Simplifying developer experience with variables and JSONata in AWS Step Functions | AWS Compute Blog

answered 10 months ago

EXPERT

reviewed 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.