Using Intrinsic Functions with Step Functions to Clean Json Strings
Want to learn more about AWS Lambda and .NET? Check out my A Cloud Guru course on ASP.NET Web API and Lambda.
I’m working on a longer post that shows how to string together multiple calls to .NET Core 3.1 Lambdas using Step Functions, but hit a problem with the Json processing that deserved its own post. In the next post I’ll show how to find a brewery using your IP address!
Introduction
Last year I wrote a couple of blog posts about the difficulties of working with Json in C#, those were prompted by problems I was having using Elasticsearch. You can find them here - Working with JSON in .NET, a better way? and Working with JSON in .NET, Infrastructure as Code with Pulumi.
Now the problem has arisen again when using C# Lambdas with Step Functions in AWS. To be fair, I am doing something a little unorthodox, I am using C# to call an API, then return the Json string directly from the Lambda. I am not serializing the Json into an object prior to returning it.
My Lambda calls http://httpbin.org/ip
and returns the Json string, but the Lambda output is heavily escaped "{\n \"origin\": \"44.192.77.46\"\n}\n"
, and
the Step Function is not able to find the Json nodes using the normal syntax $.Payload.origin
.
Intrinsic Functions
Fortunately, Step Functions have what is called intrinsic functions that allow processing of the input and outputs.
In my scenario I need to turn my string back into cleaner Json, so I use -
"ResultSelector": {
"Payload.$": "States.StringToJson($.Payload)"
}
With the intrinsic function, this is the output -
{
"Payload": {
"origin": "204.236.244.121"
}
}
But without the intrinsic function the output of the step would have been -
{
"Payload": "{\n \"origin\": \"204.236.244.121\"\n}\n"
}
What’s Next?
Using Step Functions to orchestrate calls to APIs to find breweries by IP address :)