Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW

How to convert an array/list of values into an object with key/values in an automation

No ratings
cancel
Showing results for 
Search instead for 
Did you mean: 
AfeefaTk
Support
Support

How to convert an array/list of values into an object with key/values in an automation

Last Update:

Sep 30, 2021 8:25:41 AM

Updated By:

AfeefaTk

Created date:

Jun 4, 2021 9:59:37 AM

Attachments

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

 

 

Tags (1)
Labels (2)
Version history
Last update:
‎2021-09-30 08:25 AM
Updated by: