How to convert String to dictionary

Hello everyone,

I need help for the below requirement :
I have key value pair like {“1”,“Inv-01”},{“2”,“Inv-02”}, in future we may get some more like this key value pair. I am storing this value in config as {{“1”,“Inv-01”},{“2”,“Inv-02”} }. Is there any way i can convert this({{“1”,“Inv-01”},{“2”,“Inv-02”} }) to dictionary. If yes, Please let me know how to do this.

Appreciate for the replies…!!

Thanks,
Shivappa

Hi @shivarajvp555

1.Use Assign activity and give below condition:

outputDict = New Dictionary(Of String, Object)()

  1. Use Deserialize JSON actvity and give below condition

InputString: “{‘1’:‘Inv-01’, ‘2’:‘Inv-02’}”
Type Argument: System.Collections.Generic.Dictionary(System.String,System.Object)
JsonObject: outputDict

  1. Print In message box for particular value of a key with below condition:

outputDict(“1”) (For first value)
outputDict(“2”) (For second value)

Note: Shared workflow for reference
Sequence1.xaml (7.7 KB)

Hope it helps!!
Regards,

HI,

How about the following?

dict = System.Text.RegularExpressions.Regex.Matches(yourString,"(?<={"")([^{}]*?)"",""([^{}]*?)(?=""})").Cast(Of System.Text.RegularExpressions.Match).ToDictionary(Function(m) m.Groups(1).Value,Function(m) m.Groups(2).Value)

Sample20230817-4L.zip (2.6 KB)

Regards,

1 Like

Hi @Parvathy Thanks for your quick reply…!!
My Dictionary is Dict(Of String,String) will it work in this case.?

Hi @Yoichi Thanks for your reply .!!
Can you please help me in understand what is the value used in assign acitvity of dict variable.

@shivarajvp555

Yes. It will work.

Regards,

Is your question about variable type? If so, dict is Dictionary(Of String,String)
If your question is for the expression in details, the following will help you.

System.Text.RegularExpressions.Regex.Matches(yourString,"(?<={"")([^{}]*?)"",""([^{}]*?)(?=""})")

returns content of {“” and “”}. and string before comma as group1 and string after comma as group2.

Then .Cast(Of System.Text.RegularExpressions.Match).ToDictionary(Function(m) m.Groups(1).Value,Function(m) m.Groups(2).Value)

returns Dicitonary which key is gropu1 and value is group2 (using LINQ ToDictionary method).

Regards,

1 Like

Thanks @Yoichi

This worked perfectly as per my requirement.

Thanks,
Shivappa

1 Like

Hi @Yoichi

Adding on to the above question, Encountered issue for one of the sceanrio of similar kind,my string is like {{“”&“”,“”&“”},{“”‘“”,“”'“”}} i have converted to dictionary. The output dict is coming as Dictionary<string, string>(2) { { “"&"”, “"&"” }, { “"'"”, “"'"” } }. I dont want to have the " \ " to be included in the dictionary. As i am searching the string in key in an input string like, if input string contains string in dict key it has to replace that with dictionary value. How to avoid this \ to make it as plane dictionary like Dictionary<string, string>(2) { { “”&“”, “”&“” }, { “”’“”, “”'“” } }.

Kindly let me know.

HI

How about either of the following expression?

System.Text.RegularExpressions.Regex.Matches(yourString,"(?<={"""")([^{}]*?)"""",""""([^{}]*?)(?=""""})").Cast(Of System.Text.RegularExpressions.Match).ToDictionary(Function(m) m.Groups(1).Value,Function(m) m.Groups(2).Value)

OR

System.Text.RegularExpressions.Regex.Matches(yourString,"(?<={""+)([^{}]*?)""+,""+([^{}]*?)(?=""+})").Cast(Of System.Text.RegularExpressions.Match).ToDictionary(Function(m) m.Groups(1).Value,Function(m) m.Groups(2).Value)

I tried this the above one the i got the result as :Dictionary<string, string>(2) { { “"&”, “&” }, { “"'”, “'” } }}. But still i have those " \ " added in the dictionary. Please have a look at the below SS:
image

Hi,

Now, I can see \ character. What is your source data, excel ,text etc? Can you share your source data as file if possible? It’s no problem dummy data if the problem is reproduced.

Regards,

I cannot share the file, i will share the screenshot :

Source data is text as shown in screenshot. and the first string of second group is having one " ’ "(aphastrophe)

HI,

image

Is this from immediate panel? If so, it’s C# style expression and there is no backsklash in actual data. Can you try to use Writeline or WtiteTextFile activity to check the content?

Regards,

I used write line in that i got the result as below :
image
Here i printed each group of key value pair and Key of each group to the output panel. Where each group of string is starting with " .

Hi,

Can you check the following sample?

Sample20230817-5L.zip (2.6 KB)

Regards,

Hi @Yoichi , the first solution you gave me worked perfectly, when i try to print the key from dictionary it was coming with double quotes at the start and end like “&” i have replace that quotes then it worked perfectly fine.

Appreciate you time and quick response.

Thanks.
Shiva

1 Like

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