This article describes how can a list of items be converted into a JSON object with key/values. For eg if we have an array of keys and values lets say
keys : [key1,key2,key3,key4]
values : [a,b,c,d]
which needs to converted to JSON object as below
{ "key1" : "a" , "key2" : "b" , "key3":"c", "key4":"d"}
Inorder to achieve this we can declare two variables of type string whose values will be array of keys ( [key1,key2,key3,key4] ) and values ( [a,b,c,d] ).Then we will trim square brackets of an array using below formula
{ltrim: {rtrim: {$.objectValue}, ']'}, '['}
Now the above-trimmed value can be then passed to the variable list after exploding commas by using the below formula
{explode: {$.objectValueTrim}, ','}
We will use a loop block in order to loop variable of type object through the list of keys and setting key-values to this variable object after each iteration of the loop. In order to have the proper mapping of key-values, we will declare a variable called the count of type number before the loop block which will be initially set to 0. After every iteration of the loop, we will increment the count by adding 1 to the count variable.
Formula to be used within the key of the object: {$.loop.item}
Formula to be used within the value of the object : {$.objectValueList[{ $.count }]}
Please check out the attached JSON file containing an example automation workspace demonstrating the above use case
Follow the steps provided in this article Upload Automation Workspace to import the automation from shared JSON file