Removing curly brackets after using "to.String()" across a group of fields

to.String() works as expected for a single field however for multiple fields together, I’m getting an additional { } as seen in the snaps below, would you be able to help me get rid of this?

(1) Single Field without using to.String()

(2) Single field with to.String()

(3) Multiple fields in group without to.String()

(4) Single field from a group with to.String()

(5) Multiple fields in group with to.String() [The extra curly brackets are in this snap which needs to be removed]

Should I go ahead with the route of using another mapper after this and replacing ‘{’ with “”?

Thanking in advance for your time and help :slight_smile:

Regards,
Darsh

Hey @darshthakkar,

I suppose you can remove the curly brackets from your final string, but if you want to go and make this solution totally dynamic, than I suggest to remove the brackets on an object level with the following expression:

$group.map(x=>x.toString().substring(1,x.toString().length-1)).toString()

What we’re doing here is first we’re mapping/iterating through the array and turning every object in it into a string:

	[{
		"name": "John",
		"age": 33
	},
	{
		"name": "Jack",
		"age": 23
	},
	{
		"name": "Jill",
		"age": 43
	}]

will become:

[
"{name=John, age=33}"
"{name=Jack, age=23}"
"{name=Jill, age=43}"
]

In the same iteration we’re removing the first and last character which are the object curly brackets. The rest is clear, just concatenate the strings in the array:

[
"name=John, age=33"
"name=Jack, age=23"
"name=Jill, age=43"
]

End result:

name=John, age=33,name=Jack, age=23,name=Jill, age=43

With this we’re excluding any possibility of removing a curly brackets which are in the values of the JSON objects and should not be excluded.

Sample:

[
	{
		"name": "John{test}",
		"age": 33
	},
	{
		"name": "Jack{test}",
		"age": 23
	},
	{
		"name": "Jill{test}",
		"age": 43
	}
]

Result while replacing curly brackets on final string:
name=Johntest, age=33,name=Jacktest, age=23,name=Jilltest, age=43

Result while removing curly brackets on object level with mapping:

name=John{test}, age=33,name=Jack{test}, age=23,name=Jill{test}, age=43

Regards,
Bojan

1 Like

Thank you @bojanvelevski for the detailed solution and steps.
I was planning to use column.replace() instead of column.replaceAll() however I see your point, for removing the opening {, column.replace() would have worked seamlessly but for ending }, column.replace() might have removed the first occurrence of } instead of the actual end }.

Thank you again for the explanation, really means a lot to me. I will try the above-mentioned steps and keep you posted on how it goes…

1 Like

@bojanvelevski: Let’s assume that I’ve to remove all the existing { }, should we be using the column.replaceAll() then?

Really appreciate your help and insights into this one.

Thanks!
Regards,
DT

1 Like

Yes, there’s no need for further complications, replaceAll will do the job.

1 Like

Thanks @bojanvelevski