cancel
Showing results for 
Search instead for 
Did you mean: 

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

darshthakkar
Valued Contributor

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()
image

(2) Single field with to.String()
image

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

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

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

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

Thanking in advance for your time and help 🙂

Regards,
Darsh

1 ACCEPTED SOLUTION

bojanvelevski
Valued Contributor

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

View solution in original post

5 REPLIES 5

bojanvelevski
Valued Contributor

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

darshthakkar
Valued Contributor

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…

@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

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