extract data from string

Certified Senior Developer

hi,

i want to know is there any way we can extract data from this kind of string.

"[place_id:519fcdaacfd556c0bf591a4bfd61f0c04940f00101f9014600010000000000c002089203064c6f6e646f6e,address_line1:London,city:London,address_line2:ENG, United Kingdom,country:United Kingdom,state:England,county:Greater London,formatted:London, ENG, United Kingdom]"

i want to get data as

{

place_id:"519fcdaacfd556c0bf591a4bfd61f0c04940f00101f9014600010000000000c002089203064c6f6e646f6e", 

address_line1: "London",

city: "London",

address_line2: "ENG, United Kingdom",

country: "United Kingdom",

state: "England",

county: "Greater London",

formatted: "London, ENG, United Kingdom"

}

        Discussion posts and replies are publicly visible

      • 0
        Certified Senior Developer

        Hi ,

        Can you tell from where this string is coming?

      • 0
        Certified Senior Developer

        Hi ,
        you can utilize replace function to format existing string together with keyval("hello=alpha][goodbye=beta]", {"hello"}, "=", "]") to have this as result

      • 0
        Certified Senior Developer

        Hi ,

        I am not sure, from where are you getting the input, and if the keys would always be same or different. 
        Considering the input, keys and format same,
        You can try the below code 

        a!localVariables(
          /*The string input*/
          local!string: "[place_id:519fcdaacfd556c0bf591a4bfd61f0c04940f00101f9014600010000000000c002089203064c6f6e646f6e,address_line1:London,city:London,address_line2:ENG, United Kingdom,country:United Kingdom,state:England,county:Greater London,formatted:London, ENG, United Kingdom]",
          /*The Keys*/
          local!keys: {
            "place_id",
            "address_line1",
            "city",
            "address_line2",
            "country",
            "state",
            "county",
            "formatted"
          },
          /*Removing the [ , ]*/
          local!strippedString: stripwith(local!string, "[]"),
          /*Storing the patterns in a variable*/
          local!patterns: {
            "place_id:[^,]+",
            "address_line1:[^,]+",
            "city:[^,]+",
            "address_line2:[^,]+,[^,]+,",
            "country:[^,]+",
            "state:[^,]+",
            "county:[^,]+",
            "formatted:[^,]+,[^,]+,[^,]+"
          },
          local!subStrings: a!flatten(
            a!forEach(
              items: local!patterns,
              expression: regexsearch(
                pattern: fv!item,
                searchString: local!strippedString,
                regexFlags: "g"
              ).match,
        
            )
          ),
          createdictionary(
            local!keys,
            a!flatten(
              a!forEach(
                items: local!keys,
                expression: keyval(
                  local!subStrings[fv!index],
                  fv!item,
                  ":",
                  ""
                )
              )
            )
          )
        )