Translation map defaults help

Greetings. I have a data action that will return an array of objects. In some cases not all of the keys have a value set so the API will not return that key in the object. For example:

{
"results": [
{
"key1": 0000,
"key2": 111,
"key3": "2222222222",
"key4": "3333333333",
"key5": "44444",
"key6": "5555",
"key7": "66666"
},
{
"key1": 0000,
"key2": 11111,
"key3": "2222222222",
"key4": "3333333333",
"key5": "4444",
"key6": "55555"
}
]
}

As you can see in the second object the API just does not include the key, key7 in this case, if there is no value for it. I want to set up the translation map so that the data action will fill in a default value if a key is not present. What I would like to see is something like:

{
"results": [
{
"key1": 0000,
"key2": 111,
"key3": "2222222222",
"key4": "3333333333",
"key5": "44444",
"key6": "5555",
"key7": "66666"
},
{
"key1": 0000,
"key2": 11111,
"key3": "2222222222",
"key4": "3333333333",
"key5": "4444",
"key6": "55555",
"key7": "no value"
}
]
}

Currently the translation map is

{
"translationMap": {
"data": "$.*"
},
"translationMapDefaults": {},
"successTemplate": "{"results": ${data}}"
}

I have tried multiple variations to get the output like I have above but so far no luck. Any input is appreciated.

Thank you.

1 Like

Hi scm_pa,

Is there a fixed number of results in that array, or can it vary? Assuming that it varies, I don't have a very good data action translation map solution to this off the top of my head.

JSONPath only acts as a filter, it has no concept of "set a default value if something doesn't exist". If you know how many results to expect in the array we could come up with a brute force solution where there is a default for every single key in every result, but that wouldn't be much fun to write or maintain.

A different approach would be to have the data action call a lambda to get this data, and have the lambda add the "no value"s as needed.

--Jason

Hi Jason. Thanks for the reply. Yes, the number of results in the array can vary. Using a Lambda function for this is not in the cards for this project at this time but the idea is something I will put in the mental toolbox for the next time something like this comes up.

At this time in the flow we are doing checks for not set when accessing the results from the data action. We were just hoping that we could add in default values in the translation map to safeguard against other developers forgetting to do a validity check somewhere else down the line.

Regards.

This definitely isn't the first time that something like this has come up. Please look for a / make a suggestion on our ideas site for this feature.

https://genesyscloud.ideas.aha.io/ideas

--Jason

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.