This is the input json. In this example json the key/value… "foo:bar" keeps repeating randomly. Order is not important eventhough it looks to be repeating alternately.
[
{
"foo": "bar",
"id": "baz"
},
{
"thud": "grunt",
"id": "fum"
},
{
"foo": "bar",
"id": "noot"
},
{
"zot": "toto",
"id": "pluto"
},
{
"foo": "bar",
"id": "toto"
}
]
Whenever a key/value gets repeated, rather than removing it, would want to add an additional key/value into that particular element as shown below
The desired output would be:
[
{
"foo": "bar",
"id": "baz"
},
{
"thud": "grunt",
"id": "fum"
},
{
"foo": "bar",
"id": "noot",
"desc": "1st duplicate found
},
{
"zot": "toto",
"id": "pluto"
},
{
"foo": "bar",
"id": "toto",
"desc": "2nd duplicate found"
}
]
Again order and numbering is not relevant/required. Added it for articulation purposes only
Found several solution to remove duplicates but unable to make any headway to resolve this
Appreciate any proposed resolution for above
Thanks much for you time
Tried complex solution to split the json into two and merge with -n and argjson without much break through
2
Answers
Here’s one approach using
tostream
andfromstream
to deconstruct and reconstruct the input via stream representation, which is a stream of arrays containing a path and its corresponding value. Aforeach
loop iterates over this streams, replicating each item for later reconstruction. Additionally, it keeps track of each path-value pair reduced by the path’s first item (matches occur irrelevant of their position in the original input array), and registers each appearance using a counter. If that is higher than one, also output another item (distinguished by adding_dup
to the last path item) with the current count as value.Demo
Here’s an easy-to-understand solution, or at least it’s a solution to one interpretation of the problem. It has the advantage that it can easily be modified in accordance with other interpretations of the problem.
Sample output: