How to convert into an array of string into Key/Value format?

Certified Associate Developer

Hello All,

 

Can someone please tell how we can make Key/Value from an array of string? I have an array lets say "arr1" containing the below value

 

arr1[1] Name:XYZ

arr2[2] Age:23, etc

 

Here Name and age is not declared as a variable, They act as string only. I want to make Name and Age as variables with respect to their stored values as "XYZ" and "23". So that I can easily call the values by calling Name or Age.

  Discussion posts and replies are publicly visible

Parents
  • Do you mean your array is as:

    arr1:{Name: "XyZ", Name: "Abc", Name: "Def"}
    arr2:{Age: 25, Age: 23, Age: 32}

    And you want to form as: arr3 : {{Name: "XyZ", Age: 25} , {Name: "Abc", Age: 23}, {Name: "Def", Age: 32}}
  • 0
    Certified Associate Developer
    in reply to Ravi Roshan
    No, Let me explain in detail. I have a url from which I have to fetch required values. Lets say my url is as below

    <a title='Welcome to appian' href:'https://../../Name=XYZ/Age=23/Profession=Student>

    As you can see in the above url Name,Age and Profession as the data that is being appended to it from process model. All I have to do is to fetch the values from this url and pass it as a parameter on another process model where there is a start form configured to show all this values. Now I already separated the values from this url. In such a way that when users click on this url the required value will be save in one of the array variable as "Name:XYZ ; Age:23 ; profession:student. As I want to send this values to my process model. I need to map Name,Age and Profession with the same variable available in that process model, So that the form will display corresponding value. I hope my requirement is now clear
  • You can use wherecontains fn and find where Name is there in you array. Once you find Name you can use split fn and take second part after ':'.
    I hope this is helpful to you
  • 0
    Certified Associate Developer
    in reply to TJ
    Hello TJ,

    Thanks for your response but I don't think this solution will going to be helpful. Url consists of data that is dynamic so lets say at one time the data will be "Name,Age and Profession" then other time it will be something else like "Department,Email" etc. So I don't want to point out any specific variable, I want to send as an whole the data contained in an array variable . In such a way that process model will accept the required data and ignore any other data which is not needed.
  • Hi Vivek,

    Please find the sample code,

     

    =with(
      
      local!Input_txt:"Name=XYZ/Age=23/Profession=Student",
      
      a!forEach(
        split(
          local!Input_txt,"/"
        ),
        with(
          local!ItemValues: split(fv!item,"="),
          {
            index(local!ItemValues,1,""),
            index(local!ItemValues,2,"")
          }
        )
      )
      
    )

  • Please check the below code:

    load(
    local!str: "Name:xyz/age:30/profession:Student",
    local!concatedStr: concat(local!str,"/"),
    local!name: extract(local!concatedStr, "Name:", "/"),
    local!age: extract(local!concatedStr, "age:", "/"),
    local!profession : extract(local!concatedStr, "profession:", "/"),
    local!array: {
    Name: local!name,
    Age: local!age,
    Profession: local!profession
    },
    local!array
    )
  • Hello Vivek,

    See below code, it may help you develop a more robust solution. I've quickly thrown together some code which will create a list of list of strings, which should a be a list of key/value pairs where the keys can be dynamic. Please note you may want to do some additional string cleaning, as the last value has the '>' character still in it.

    load(
    local!data: "<a title='Welcome to appian' href:'https://../../Name=XYZ/Age=23/Profession=Student>",
    local!subdata: keyval(
    local!data,
    "=",
    "/"
    ),
    local!splitdata: split(
    local!subdata,
    "/"
    ),
    local!cleanSplitData: filter(
    search(
    "=",
    _,
    1
    ),
    local!splitdata
    ),
    /*If '=' is not found, search returns 0, which converts to false, and so those strings are removed from list*/
    local!groupedData: apply(
    split(
    _,
    "="
    ),
    local!cleanSplitData
    ),
    local!groupedData/*indexing this list will give you a pair, where [1] is the key, and [2] is the value*/

    )
Reply
  • Hello Vivek,

    See below code, it may help you develop a more robust solution. I've quickly thrown together some code which will create a list of list of strings, which should a be a list of key/value pairs where the keys can be dynamic. Please note you may want to do some additional string cleaning, as the last value has the '>' character still in it.

    load(
    local!data: "<a title='Welcome to appian' href:'https://../../Name=XYZ/Age=23/Profession=Student>",
    local!subdata: keyval(
    local!data,
    "=",
    "/"
    ),
    local!splitdata: split(
    local!subdata,
    "/"
    ),
    local!cleanSplitData: filter(
    search(
    "=",
    _,
    1
    ),
    local!splitdata
    ),
    /*If '=' is not found, search returns 0, which converts to false, and so those strings are removed from list*/
    local!groupedData: apply(
    split(
    _,
    "="
    ),
    local!cleanSplitData
    ),
    local!groupedData/*indexing this list will give you a pair, where [1] is the key, and [2] is the value*/

    )
Children
No Data