Mail json to JSON

Hi Community,
Below is my input text

Adress: hej123
Information: hallo how 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.

from the above mail i want a JSON string like below
“[{"FullName":"Alex Doe","PhoneNumber":"1231453","Day":"true","Night":"true"},{"FullName":"Bob Doe","PhoneNumber":"1231515123","Day":"false","Night":"false"},{"FullName":"Carol Doe","PhoneNumber":"33333333","Day":"true","Night":"false"},{"FullName":"Dave Doe","PhoneNumber":"1234","Day":"false","Night":"true"}]”

Kindly help me with it

Hey @Chirag12
First, read the text input that contains the information about each person


Use an Assign activity to split the inputText into individual lines, which makes parsing easier:
linesArray = inputText.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)

Next, use LINQ to extract information from each line that contains data about a person.

personList = (From line In linesArray
              Where line.StartsWith("person")
              Let parts = line.Split(","c)
              Let namePart = parts(0).Split(":"c)(1).Trim()
              Let phonePart = parts(1).Split(":"c)(1).Trim()
              Let dayNightPart = If(parts.Length > 2, parts(2).Trim().ToLower(), "")
              Let day = If(dayNightPart = "day_night" Or dayNightPart = "day", "true", "false")
              Let night = If(dayNightPart = "day_night" Or dayNightPart = "night", "true", "false")
              Select New Dictionary(Of String, String) From {
                  {"FullName", namePart},
                  {"PhoneNumber", phonePart},
                  {"Day", day},
                  {"Night", night}
              }).ToList()

  • From line In linesArray: Iterate over each line in linesArray.
  • Where line.StartsWith("person"): Filter lines that start with "person". This ensures we only process relevant lines.
  • Let parts = line.Split(","c): Split each line by the comma to separate different pieces of data (FullName, PhoneNumber, etc.).
  • Let namePart = parts(0).Split(":"c)(1).Trim(): Extract the person’s name by splitting the first part of the line after the colon (:) and trimming any spaces.
  • Let phonePart = parts(1).Split(":"c)(1).Trim(): Extract the phone number similarly.
  • Let dayNightPart = If(parts.Length > 2, parts(2).Trim().ToLower(), ""): Check if there’s information about day/night availability, and extract it if present.
  • Let day = If(dayNightPart = "day_night" OrElse dayNightPart = "day", "true", "false"): Determine if the person is available during the day.
  • Let night = If(dayNightPart = "day_night" OrElse dayNightPart = "night", "true", "false"): Determine if the person is available at night.
  • Select New Dictionary(Of String, String) From { ... }: Create a dictionary for each person with keys like "FullName", "PhoneNumber", "Day", "Night".

The result personList is a List(Of Dictionary(Of String, String)).

Now, we are building JSON manually
jsonResult = "["
Use For Each to Iterate Through personList


jsonResult & "{""FullName"":""" & person("FullName") & """,""PhoneNumber"":""" & person("PhoneNumber") & """,""Day"":""" & person("Day") & """,""Night"":""" & person("Night") & """},"

After the loop, use an Assign activity to remove the trailing comma and add the closing bracket
jsonResult = jsonResult.TrimEnd(","c) & "]"

Whole flow looks that:

here you will find project of this solution:
BlankProcess2.zip (3.3 KB)