Skip to main content
Participating Frequently
Dec 27, 2024 4:25 AM UTC
Question

How to handle "[key1:value1,key2:value2]"

I have got a input string like "[key1:value1,key2:value2]", how to get the value by key?

    5 replies

    New Participant
    Dec 27, 2024 7:59 AM UTC

    Hi [mention:25710eb09098448cbe7f3570455ba814:e9ed411860ed4f2ba0265705b8793d05] 

    I have developed a solution that achieve this dynamically. Below is the code for your reference.

    a!localVariables(
    local!input: "[key1:value1,key2:value2,key3:value3,key4:value4]",
    local!stripText: mid(local!input, 2, len(local!input) - 2),
    local!tempString: split(split(local!stripText, ","), ":"),
    local!dict: a!map(),
    local!updatedMaps: a!forEach(
    items: local!tempString,
    expression: if(
    mod(fv!index, 2) = 0,
    {},
    a!update(
    local!dict,
    fv!item,
    index(local!tempString, fv!index + 1, null())
    )
    )
    ),
    local!keys: a!flatten(
    a!forEach(
    items: local!updatedMaps,
    expression: a!keys(fv!item)
    )
    ),
    a!update(
    a!map(),
    local!keys,
    a!forEach(
    items: local!updatedMaps,
    expression: index(fv!item, a!keys(fv!item), null)
    )
    )
    )

    Known Participant
    Dec 27, 2024 9:08 AM UTC

    expecting this might be CDT value processed! Instead of working on the value, try working on it at its original CDT form.

    stefanhelzle0001
    Brainy
    Dec 27, 2024 10:34 AM UTC

    Where do you get this value from?

    kalpeshg0081
    Participating Frequently
    Dec 31, 2024 6:12 PM UTC

    [mention:25710eb09098448cbe7f3570455ba814:e9ed411860ed4f2ba0265705b8793d05] , you can use the below code to convert you input string to a valid list of Map type variable and thn by using simple index function you can get the values based on the keys.

    a!localVariables(
      local!input: substitute(
        substitute(
          "[key1:value1,key2:value2,key3:value3,key4:value4]",
          "[",
          ""
        ),
        "]",
        ""
      ),
      local!arrayOfText: split(local!input, ","),
      local!finalMap: a!flatten(
        a!forEach(
          items: local!arrayOfText,
          expression: a!localVariables(
            local!temp: split(fv!item, ":"),
            {
              a!map(key: local!temp[1], Value: local!temp[2])
            }
          )
        )
      ),
      index(
        local!finalMap.Value,
        wherecontains("key1", local!finalMap.key),
        ""
      )
    )

    New Participant
    Jan 8, 2025 10:38 AM UTC

    If you can be sure that values won't have special character "]", you can use below code to get values.