i want to remove the duplicates from each array in this json:
{
"abc": [
"five"
],
"pqr": [
"one",
"one",
"two",
"two",
"three",
"three",
"four",
"four"
],
"xyz": [
"one",
"one",
"two",
"two",
"four"
]
}
output I am expecting after removing the duplicates:
{
"abc": [
"five"
],
"pqr": [
"one",
"two",
"three",
"four"
],
"xyz": [
"one",
"two",
"four"
]
}
i tried map, uniq, group_by with jq
but nothing helped
2
Answers
unique
can remove duplicates, but it automatically sorts the arrays, which may or may not be what you want.Demo
You can retrieve the original ordering by recreating the array based on
sort
ing theindex
positions of all of itsunique
items:Demo
Or circumvent any sorting behaviour by writing your own straightforward de-duplication function:
Demo
In my tests, the latter performed best, with both producing
Here is a sort-free alternative for obtaining the distinct items in an array (or stream) while retaining the order of first occurrence.
It uses a filter that is a tiny bit more complex than it would otherwise be, for the sake of complete genericity:
Now it’s just a matter of applying this filter to your JSON. One possibility would be: