Forum Discussion
OK. I finally got this to work, but the method I used seems kind of overlong and hacky. First I took the input and ran it through this:
JSON.stringify(jsonPath($, "entity.feed.entry[*].content['$']")).replaceAll('\\n','').replaceAll('\\','').replaceAll('"{','{').replaceAll('}"','}')
This cleaned up the string to look like clean JSON minus the \n, escape and extra quote characters.
After this I turned it into a document and read it back in and used a JSON parser.
This worked, but I’m not really happy with the method. Is there any sort of clean function that takes control characters and escapes and just scrubs them? I’ve seen payloads in the past that come in a single field in a REST call and it seems like I’m doing a lot of extra and unnecessary work if I have to write it to disk just to read it back in.
The expression language is a subset of JavaScript, so you can usually start by looking for JavaScript-based solutions.
In this case, take a look at this post:
The first answer has the simplest solution. The only change you have to make is to convert the callback function to use the arrow-function syntax, like so:
$arr.filter((item, pos, a) => a.indexOf(item) == pos)
- mohamadelmardin9 years agoNew Contributor III
Thank you. That was helpful :+1:
Thanks @tstack! For anyone wondering what this looks like in the Designer::
Pipeline .slp file: 20170404_mapper-distinct-elements_2017_04_04.slp (3.0 KB)
Hi, I have an complex JSON array and I have to remove the duplicates based on a field. For Ex:
[ {PartnerID:1127839, ProductNum:a, …} ,
{PartnerID:1127839, ProductNum:a,…} ,
{PartnerID:1127839, ProductNum:b,…} ,
{PartnerID:1127839, ProductNum:c,…} ],
How can I remove the duplicate array elements based on the ProductNum Field to produce the below JSON
[ {PartnerID:1127839, ProductNum:a, …} ,
{PartnerID:1127839, ProductNum:b,…} ,
{PartnerID:1127839, ProductNum:c,…} ]I have tried the above logic and it didnt work, as other fields in this json structure.
Same idea, except you’d want to use the ‘findIndex()’ method with a custom predicate instead of the ‘indexOf()’ method, like so:
$arr.filter((item, pos, a) => a.findIndex(elem => item.ProductNum == elem.ProductNum) == pos)
So, the ‘findIndex()’ method will walk through the array and evaluate the predicate function (elem => item.ProductNum == elem.ProductNum) on every element in the array. If a match is found, the index is returned and, if it’s the same index as the current element being examined in the filter, then we add the element to output list.