Regex to extract data from string and make it json format

I have a text like this:

Adress: hej123
Information:

person 1: Alex Doe, phone1: 1231453, day_night

person 2: Bob Doe, phone1: 1231515123, day_night

person 3: Carol Doe, phone1: 33333333, day

person 4: Dave Doe, phone1: 1234, night

and i need to make it as a json format like this:

[{“FullName”:“Alex Doe”,“PhoneNumber”:“1231453”,“Day”:true,“Night”:true},{“FullName”:“Bob Doe”,“PhoneNumber”:“1231515123”,“Day”:true,“Night”:true},{“FullName”:“Carol Doe”,“PhoneNumber”:“33333333”,“Day”:true,“Night”:false},{“FullName”:“Dave Doe”,“PhoneNumber”:“1234”,“Day”:false,“Night”:true}]

Can anybody help please? :smiley:

@christian.bendtsen,

Use this Regex to retrieve the data in matches.

person \d+: (?<FullName>[^,]+), phone1: (?<PhoneNumber>[^,]+), (?<DayNight>[^,\r\n]+)

image

Create one string list before for each loop
jsonList

Iterate through matches using For Each loop and assign variable values as below.

Assign
FullName = match.Groups("FullName").Value.Trim()
PhoneNumber = match.Groups("PhoneNumber").Value.Trim()
DayNight = match.Groups("DayNight").Value.Trim()

Assign
Day = DayNight.Contains("day")
Night = DayNight.Contains("night")

Assign
jsonObject = $"{{\"FullName\":\"{FullName}\",\"PhoneNumber\":\"{PhoneNumber}\",\"Day\":{Day.ToString.ToLower},\"Night\":{Night.ToString.ToLower}}}"

Assign
jsonList.Add(jsonObject)

Prepare final json array

Assign
jsonArray = "[" + String.Join(",", jsonList) + "]"

Thanks,
Ashok :slight_smile:

Hallo Ashokkarale!

Can you please show it with a UiPath file?

I tried what you just said, but cant make it work.

Kind regards :smiley:

Hi @christian.bendtsen

Assign -> inputText = "Adress: hej123
Information:

person 1: Alex Doe, phone1: 1231453, day_night

person 2: Bob Doe, phone1: 1231515123, day_night

person 3: Carol Doe, phone1: 33333333, day

person 4: Dave Doe, phone1: 1234, night"


Assign -> persons = inputText.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Skip(2).ToArray()

Assign -> jsonArray = New List(Of Dictionary(Of String, Object))

For Each item In persons
    Assign -> personData = item.Split(", "c)
    Assign -> fullName = personData(0).Split(": "c)(1).Trim()
    Assign -> phoneNumber = personData(1).Split(": "c)(1).Trim()
    Assign -> availability = personData(2).Trim().ToLower()
    Assign -> day = availability.Contains("day")
    Assign -> night = availability.Contains("night")

    Assign -> personDict = New Dictionary(Of String, Object) From {
        {"FullName", fullName},
        {"PhoneNumber", phoneNumber},
        {"Day", day},
        {"Night", night}
    }
    Add to collection -> jsonArray (item: personDict)
End For Each

Assign -> jsonString = JsonConvert.SerializeObject(jsonArray)

Write Text File -> FileName: "output.json", Text: jsonString

Output

VariablePanel

Hope it helps!!

Hallo Pravallikapaluri!

Thanks a lot, i can see i have a new problem now :frowning:

Sometimes a person can have more than one phonenumber.

Exemple:

Adress: hej123
Information:

person 1: Alex Doe, phone1: 1231453, day_night

person 2: Bob Doe, phone1: 1231515123, phone2: 333305483, day_night

person 3: Carol Doe, phone1: 33333333, day

person 4: Dave Doe, phone1: 1234, night

So here, person 2 Bob Doe, needs 2 lines in the json, the output should in this case be:

[{“FullName”:“Alex Doe”,“PhoneNumber”:“1231453”,“Day”:true,“Night”:true},
{“FullName”:“Bob Doe”,“PhoneNumber”:“1231515123”,“Day”:true,“Night”:true},
{“FullName”:“Bob Doe”,“PhoneNumber”:“333305483”,“Day”:true,“Night”:true},{“FullName”:“Carol Doe”,“PhoneNumber”:“33333333”,“Day”:true,“Night”:false},{“FullName”:“Dave Doe”,“PhoneNumber”:“1234”,“Day”:false,“Night”:true}]

Hope you can help me out! otherwise it is a very nice explanation you made :smiley: !

@christian.bendtsen

Assign -> inputText (String) = "Adress: hej123
Information:

person 1: Alex Doe, phone1: 1231453, day_night

person 2: Bob Doe, phone1: 1231515123, phone2: 333305483, day_night

person 3: Carol Doe, phone1: 33333333, day

person 4: Dave Doe, phone1: 1234, night"

Assign -> lines (Array of String) = inputText.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
Assign -> persons (Array of String) = lines.Skip(2).ToArray()  // Skip the first two lines: "Adress" and "Information"

Assign -> jsonArray (List(Of Dictionary(Of String, Object))) = New List(Of Dictionary(Of String, Object))

For Each item As String In persons
    Assign -> personData (Array of String) = item.Split(New String() {", "}, StringSplitOptions.None)
    Assign -> fullName (String) = personData(0).Split(": "c)(1).Trim()

    // Initialize phone numbers list
    Assign -> phoneNumbers (List(Of String)) = New List(Of String)
    
    // Extract phone numbers
    For Each data As String In personData
        If data.StartsWith("phone1:") OrElse data.StartsWith("phone2:") Then
            // Use Invoke Method activity to add phone numbers to the list
            Invoke Method -> TargetObject: phoneNumbers
                           MethodName: "Add"
                           Parameters: In (String): data.Split(": "c)(1).Trim()
        End If
    Next

    Assign -> availability (String) = personData.Last().Trim().ToLower()
    Assign -> day (Boolean) = availability.Contains("day")
    Assign -> night (Boolean) = availability.Contains("night")

    // Create JSON entries for each phone number
    For Each phoneNumber As String In phoneNumbers
        Assign -> personDict (Dictionary(Of String, Object)) = New Dictionary(Of String, Object) From {
            {"FullName", CType(fullName, Object)},
            {"PhoneNumber", CType(phoneNumber, Object)},
            {"Day", CType(day, Object)},
            {"Night", CType(night, Object)}
        }
        Add to collection -> jsonArray (item: personDict)
    Next
Next

Assign -> jsonString (String) = Newtonsoft.Json.JsonConvert.SerializeObject(jsonArray)

Write Text File -> FileName: "output.json", Text: jsonString


Hope it helps!!

Hallo Again Pravallikapaluri! :smiley:

I cant make it work…

Problem is, that more text is appearing.
There will be between 1-4 person pr. text message
I’ll give an example for the text indput:

Exemple:

"Adress: hej123
Information: hallo haw are you…

person 1: Alex Doe, phone1: 1231453, day_night

person 2: Bob Doe, phone1: 1231515123, phone2: 333305483, day_night

person 3: Carol Doe, phone1: 33333333, day

person 4: Dave Doe, phone1: 1234, night

Have a good day

Kind regards ------
Adresss
City
Phonenumber
Email

You cant respond on this E-mail."

Hope you can help! :smiley:

@christian.bendtsen

I think your requirement is changing.
we got Solution for this topic created.
Close this topic and raise another question to solve the issue and some others help . It will definitely help others also to search a topic

Regards

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.