Group String in Array and Json

Hello folks,

let me explain my issue. I have an string like this:

[187597]xxxxxxxx // [66023]xxxxxxxxx // [187597]yyyyyyyyyy // [10068125]xxxxxx // [677203]xxxxx // [677203]yyyyyyyyyy // [10068125]yyyyyyyyyy //

I want convert it to Json, but is not the problem. The problem is that I want to group them by the “[number]”. I would need something like this:

{
	"187597": [
		"xxxxx",
		"yyyyy"
	],
	"66023": "xxxxx",
	"10068125": [
		"xxxxx",
		"yyyyy"
	],
	"677203": [
		"xxxxx",
		"yyyyy"
	]
}
 

So the problem is basically how can I group them by the value “[number]”. All the rest is fine (quotes, commas, tabs, etc.)

Any idea about this? :slight_smile:

Thank you very much friends.

Kind regards,
Pablo

Actually I got this Json format, maybe its worth to work with this instead work with the original String:

		{
			"187597": "xxxx",
			"66023": "xxxx",
			"187597": "yyyy",
			"10068125": "xxxx",
			"677203": "xxxx",
			"677203": "yyyy",
			"10068125": "xxxx"
		}

after deserialization will look like this:

you will lose some values.

We can achieve it by grouping the values, but need the detail input string. Maybe you can share it as txt file with us? Thanks

2 Likes

Hi @Pablo_Sanchez ,

I was able to Get the Data into the Json as Needed. Although there might be a better method than this.

First we have to identify the group of values in the input String. We could use the below Regex for that :

\[(\d+)\](\w+)

image

We can then use the Matches Activity with the above pattern to get the resultant matched values.

Here onwards, the data I have converted to a Datatable for ease in grouping the values.

Next, Group the data based on Number and iterate through the Grouped Values and add the Jobject or JArray as needed in the loop to a parent JObject.

Output would be in the Below format :
image

Check the workflow and Let us know if this doesn’t work :
ConvertString_ToJson.xaml (13.0 KB)

1 Like

Hi @ppr

Thank you for the help. Here is the .txt input file.

Regards mate
inputText.txt (257 Bytes)

Amazing @supermanPunch

Thank you very much. Gonna try it and mark it as solution if I’m able to work with it. That’s exactly what I was thinking but I dont master REGEX. Lucky I find someone who does :slight_smile:

Regards friend

1 Like

@supermanPunch is almost perfect. Well, it is really perfect but I’ve made a mistake don’t telling you that the String has blank spaces :frowning_face:

To explain me better, the initial string is not like “xxxxxxx”… is like “xxxx xxxxx xxxx xxxx xxx”, so using your .xaml i got this result

image

Could we fix this? Thank you again sir.
Regards,
Pablo

Hi @Pablo_Sanchez ,

Try Replacing the Regular expression Pattern in Matches Activity with the below :

\[(\d+)\](.*?)\/\/

Let us know if it doesn’t work

2 Likes

@Pablo_Sanchez
With the help of Regex we can split the string


refering to the groups (we will trim later)
grafik

For regex learning have a look here:

With following flow we can achieve the result:
grafik

grafik

The main idea is about:

  • use regex to as mentioned above
    Regex.Matches(strText, strPattern, RegexOptions.IgnoreCase)

  • group the matches and create JProperties, return all as an array

(From m In myMatches.Cast(Of Match).toList
Group m By k=m.Groups(1).Value.Trim Into grp = Group
Let arrVal = grp.Select(Function (a) a.Groups(2).Value.Trim).toArray
Let ja = JArray.FromObject(arrVal)
Select p = New JProperty(k, ja)).toarray
  • Create the JObject from the returned JProperty Array
    new JObject(arrProps)

We had used LINQ:

But can also decompose the LINQ part to essential activities

Find starter help here:
ppr_RegexBox_PabloSanchez.xaml (7.0 KB)

2 Likes

Thank you very much guys, I wish I could give 2 solutions xD

That’s what makes UiPath big… its community!

Thanks thanks and thanks

Regards,
Have a nice day guys.

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