# Group String in Array and Json

**URL:** <https://forum.uipath.com/t/group-string-in-array-and-json/407120>\
**Category:** Studio\
**Tags:** array, string, json, system, webapi, activities\
**Created:** [March 15, 2022, 9:33am UTC](https://forum.uipath.com/t/group-string-in-array-and-json/407120 "2022-03-15T09:33:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pablo\_Sanchez](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/pablo_sanchez/32/14355_2.png) [@Pablo\_Sanchez](https://forum.uipath.com/u/Pablo_Sanchez)\
**Post date:** [March 15, 2022, 9:33am UTC](https://forum.uipath.com/t/group-string-in-array-and-json/407120/1 "2022-03-15T09:33:02Z")

</div>

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:

```auto
{
	"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? 🙂

Thank you very much friends.

Kind regards,  
Pablo

---

<div class="post-metadata">

**Author:** ![Pablo\_Sanchez](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/pablo_sanchez/32/14355_2.png) [@Pablo\_Sanchez](https://forum.uipath.com/u/Pablo_Sanchez)\
**Post date:** [March 15, 2022, 9:36am UTC](https://forum.uipath.com/t/group-string-in-array-and-json/407120/2 "2022-03-15T09:36:54Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![ppr](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/ppr/32/464097_2.png) [@ppr](https://forum.uipath.com/u/ppr)\
**Post date:** [March 15, 2022, 10:53am UTC](https://forum.uipath.com/t/group-string-in-array-and-json/407120/3 "2022-03-15T10:53:03Z")

</div>

> [@Pablo\_Sanchez](#):
>
> ```auto
> {
> "187597": "xxxx",
> "66023": "xxxx",
> "187597": "yyyy",
> "10068125": "xxxx",
> "677203": "xxxx",
> "677203": "yyyy",
> "10068125": "xxxx"
> }
> 
> ```

after deserialization will look like this:

 ![grafik](https://global.discourse-cdn.com/uipath/original/4X/5/a/5/5a5dec2652e59c249c2a1d5bbe73230d7b52f6c2.png)

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

---

<div class="post-metadata">

**Author:** ![supermanPunch](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/supermanpunch/32/133652_2.png) [@supermanPunch](https://forum.uipath.com/u/supermanPunch)\
**Post date:** [March 15, 2022, 10:59am UTC](https://forum.uipath.com/t/group-string-in-array-and-json/407120/4 "2022-03-15T10:59:02Z")

</div>

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 :

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

```

![image](https://global.discourse-cdn.com/uipath/original/4X/d/e/0/de012ddd64f333f2748ca40775b92848538d03cc.png)

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.

 ![image](https://global.discourse-cdn.com/uipath/original/4X/c/0/1/c0105f83638371825909fc1c02c32b5fc7cd9544.png)

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.

 ![image](https://global.discourse-cdn.com/uipath/original/4X/c/2/6/c26f112e37bbfda15cfc2b265236d3909daa6cd8.png)

Output would be in the Below format :  
 ![image](https://global.discourse-cdn.com/uipath/original/4X/3/1/7/3177ddf4d1d76fbd77b8cd47f25abbfc44cdd4b5.png)

Check the workflow and Let us know if this doesn’t work :  
[ConvertString\_ToJson.xaml](https://forum.uipath.com/uploads/short-url/eFSPn2RH3Ictf9YhC8yXxqxheqn.xaml) (13.0 KB)

---

<div class="post-metadata">

**Author:** ![Pablo\_Sanchez](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/pablo_sanchez/32/14355_2.png) [@Pablo\_Sanchez](https://forum.uipath.com/u/Pablo_Sanchez)\
**Post date:** [March 15, 2022, 10:59am UTC](https://forum.uipath.com/t/group-string-in-array-and-json/407120/5 "2022-03-15T10:59:06Z")

</div>

Hi @ppr

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

Regards mate  
[inputText.txt](https://forum.uipath.com/uploads/short-url/r19GalJzkQ3nBTZStHsw32jPRMv.txt) (257 Bytes)

---

<div class="post-metadata">

**Author:** ![Pablo\_Sanchez](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/pablo_sanchez/32/14355_2.png) [@Pablo\_Sanchez](https://forum.uipath.com/u/Pablo_Sanchez)\
**Post date:** [March 15, 2022, 11:00am UTC](https://forum.uipath.com/t/group-string-in-array-and-json/407120/6 "2022-03-15T11:00:17Z")

</div>

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 🙂

Regards friend

---

<div class="post-metadata">

**Author:** ![Pablo\_Sanchez](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/pablo_sanchez/32/14355_2.png) [@Pablo\_Sanchez](https://forum.uipath.com/u/Pablo_Sanchez)\
**Post date:** [March 15, 2022, 11:14am UTC](https://forum.uipath.com/t/group-string-in-array-and-json/407120/7 "2022-03-15T11:14:45Z")

</div>

@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 ☹

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](https://global.discourse-cdn.com/uipath/original/4X/3/7/2/3721dc3010a9d616267b11eb31fe4cd01ef6844e.png)

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

---

<div class="post-metadata">

**Author:** ![supermanPunch](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/supermanpunch/32/133652_2.png) [@supermanPunch](https://forum.uipath.com/u/supermanPunch)\
**Post date:** [March 15, 2022, 11:20am UTC](https://forum.uipath.com/t/group-string-in-array-and-json/407120/8 "2022-03-15T11:20:28Z")

</div>

Hi @Pablo_Sanchez ,

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

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

```

Let us know if it doesn’t work

---

<div class="post-metadata">

**Author:** ![ppr](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/ppr/32/464097_2.png) [@ppr](https://forum.uipath.com/u/ppr)\
**Post date:** [March 15, 2022, 11:25am UTC](https://forum.uipath.com/t/group-string-in-array-and-json/407120/9 "2022-03-15T11:25:45Z")

</div>

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

 ![grafik](https://global.discourse-cdn.com/uipath/original/4X/3/5/f/35f43479cebffb9f07597e520f117d7a2fa27470.png)  
refering to the groups (we will trim later)  
 ![grafik](https://global.discourse-cdn.com/uipath/original/4X/8/4/1/84142ca6304057ebfa9c577c2d963d72f0c6abb0.png)

For regex learning have a look here:

> [@owl bookmark \[CheatSheet\] - System.Text.RegularExpressions | RegEx](https://forum.uipath.com/t/cheatsheet-system-text-regularexpressions-regex/354895):
>
> This CheatSheet introduces the basic use of regex functions. With further examples also special cases are presented. Introduction From the namespace System.Text.RegularExpressions following methods are offered: Regex.Match Regex.Matches Regex.isMatch Regex.Replace Regex.Split A simple usage for example would look like this: [grafik] Recommendation: add System.Text.RegularExpressions to the imports: [grafik] it allows to use the shortened statement, as the namespace part can be ommited…

With following flow we can achieve the result:

 ![grafik](https://global.discourse-cdn.com/uipath/original/4X/f/5/e/f5eac606d0b79bf87172d025d8383cd2bd0c5526.png)

 ![grafik](https://global.discourse-cdn.com/uipath/original/4X/0/c/4/0c4bfcc45e8a5c9a70325bb4bc560d5afe9d53ea.png)

 ![grafik](https://global.discourse-cdn.com/uipath/original/4X/2/e/5/2e5bd55a4990a70dfdb4a81add58def5d389a850.jpeg)

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

```auto
(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:

> [@\[HowTo\] LINQ (VB.Net) Learning Catalogue](https://forum.uipath.com/t/howto-linq-vb-net-learning-catalogue/325671):
>
> This topic references the different Learning HowTos related to LINQ. It will be updated on new published HowTos as well LINQ Learning: Topic LINK First Start with LINQ [[HowTo] - First Start with LINQ (VB.Net)](https://forum.uipath.com/t/howto-first-start-with-linq-vb-net/318964/) Exploring the LINQ universe [[HowTo] - Exploring the LINQ Universe (VB.Net)](https://forum.uipath.com/t/howto-exploring-the-linq-universe-vb-net/325667)Operator [HowTo] Overview Topic LINK Where [[HowTo] LINQ - First Start # Where Operator](https://forum.uipath.com/t/howto-linq-first-start-where-operator/464108) Select work in progress Any [[HowTo] LINQ – First Start # Any Operator](https://forum.uipath.com/t/howto-linq-first-start-any-operator/466138) All [[HowTo] LINQ – First Start…](https://forum.uipath.com/t/howto-linq-first-start-all-operator/467292)

But can also decompose the LINQ part to essential activities

Find starter help here:  
[ppr\_RegexBox\_PabloSanchez.xaml](https://forum.uipath.com/uploads/short-url/2OyjhtSyhV9nnKmqrnUChfvfaOa.xaml) (7.0 KB)

---

<div class="post-metadata">

**Author:** ![Pablo\_Sanchez](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/pablo_sanchez/32/14355_2.png) [@Pablo\_Sanchez](https://forum.uipath.com/u/Pablo_Sanchez)\
**Post date:** [March 15, 2022, 11:33am UTC](https://forum.uipath.com/t/group-string-in-array-and-json/407120/10 "2022-03-15T11:33:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/system/32/182466_2.png) [@system](https://forum.uipath.com/u/system)\
**Post date:** [March 18, 2022, 11:34am UTC](https://forum.uipath.com/t/group-string-in-array-and-json/407120/11 "2022-03-18T11:34:20Z")

</div>

Closed on March 18, 2022, 11:34am UTC

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