JSONPath can only query your JSON, and you won't be able to add data on the fly, the default value would need to be present in your input. After many tests I was unable to find a "clean" way of doing this, but here's some methods to achieve this.
Method 1:
Since you mentioned you'll even settle for a way to not break if a parameter is not present in the input, this is actually quite easy to do, however it won't allow us to send a default value (except for an empty array) if the values are missing. To do this we can filter out specific values on our leaf nodes by specifying multiple children (['' (, '')]). For example the JSONPath "$.['Bar','Foo']" will filter out only the leaf nodes containing the nodes Bar and Foo, however it will not return an error if either or both are missing. In the case of all the children specified being missing it will return an empty array []. Here's an example State Machine to illustrate this.
{
"StartAt": "ExampleLeafNodes",
"States": {
"ExampleLeafNodes": {
"Type": "Parallel",
"Branches": [
{
"StartAt": "State1-BarPresent",
"States": {
"State1-BarPresent": {
"Type": "Pass",
"Parameters": {
"Bar": "Baz"
},
"Next": "State2-BarPresent"
},
"State2-BarPresent": {
"Type": "Pass",
"Parameters": {
"Foo.$": "$.['Bar','AnyOtherField']"
},
"End": true
}
}
},
{
"StartAt": "State1-BarNotPresent",
"States": {
"State1-BarNotPresent": {
"Type": "Pass",
"Parameters": {
"Field1": "Value1"
},
"Next": "State2-BarNotPresent"
},
"State2-BarNotPresent": {
"Type": "Pass",
"Parameters": {
"Foo.$": "$.['Bar','AnyOtherField']"
},
"End": true
}
}
}
],
"End": true
}
}
}
Method 2:
Depending on how much control you have over the structure of the input and if you don't need to reference any other variables from the input you might be able to do this in a single State using InputPath and Parameters. The idea behind this is to get your Input to the State in the form:
"OutputArray": [
{
"Bar": {
"Value": [
"Baz"
]
}
},
{
"Foo": {
"Value": [
"DefaultValueFoo"
]
}
}
]
The first element of the array should be the value which might be missing, if the value is missing the default value (foo) will be the first element. As the first element will always be present we can use the JSONPath "$['OutputArray'][0]..Value" to get the Value of the first element. After that in Parameters we can use the JSONPath "$[0][0]" to extract the exact value. Here's an example State Machine for this example:
{
"StartAt": "State1",
"States": {
"State1": {
"Type": "Pass",
"Parameters": {
"OutputArray": [
{
"Bar": {
"Value": [
"Baz"
]
}
},
{
"Foo": {
"Value": [
"DefaultValueFoo"
]
}
}
]
},
"Next": "State2"
},
"State2": {
"Type": "Pass",
"InputPath": "$['OutputArray'][0]..Value",
"Parameters": {
"Foo.$": "$[0][0]"
},
"End": true
}
}
}