Forum Discussion

darshthakkar's avatar
darshthakkar
Valued Contributor
4 years ago
Solved

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) Si...
  • bojanvelevski's avatar
    4 years ago

    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