Skip to main content
November 3, 2023
Question

extract data from string

  • November 3, 2023
  • 3 replies
  • 0 views

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"

}

        3 replies

        sriramk5349
        November 3, 2023

        Hi [mention:50db1cd8fb304dd499245322b8cbac67:e9ed411860ed4f2ba0265705b8793d05],

        Can you tell from where this string is coming?

        November 3, 2023

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

        mudits1812
        November 3, 2023

        Hi [mention:50db1cd8fb304dd499245322b8cbac67:e9ed411860ed4f2ba0265705b8793d05],

        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,
                  ":",
                  ""
                )
              )
            )
          )
        )