@@ -25,6 +25,8 @@ def __init__(self):
2525 # Track keys at each path level to detect duplicates
2626 self .key_registry : Dict [str , Dict [str , bool ]] = {}
2727 self .current_duplicate_index : Dict [str , int ] = {}
28+ # Track seen array elements to detect duplicates
29+ self .seen_array_elements : Dict [str , List [JsonValue ]] = {}
2830
2931 def get_path_with_index (self , path : List [str ], key : str ) -> List [str ]:
3032 current_level = "." .join (path )
@@ -76,8 +78,24 @@ def traverse_array(self, items: JsonArray, path: list[str]) -> None:
7678 """Process JSON array items while updating the path for duplicates."""
7779 array_path = path [- 1 ]
7880 base_path = path [:- 1 ]
81+ seen_elements = self .seen_array_elements .setdefault ("." .join (path ), set ())
7982
8083 for idx , item in enumerate (items ):
84+ serialized_item = json .dumps (item , sort_keys = True )
85+ if serialized_item in seen_elements :
86+ element = f"{ array_path } [{ idx } ]"
87+ duplicate_path = "." .join (base_path + [element ])
88+ self .duplicate_keys_and_paths .setdefault (element , []).append (duplicate_path )
89+ print (f"Found duplicate array element at path: { duplicate_path } " )
90+ else :
91+ seen_elements .add (serialized_item )
92+
93+ # if item in seen_elements:
94+ # duplicate_path = f"{array_path}[{idx}]"
95+ # self.duplicate_keys_and_paths.setdefault(duplicate_path, []).append(f"{base_path[0]}.{duplicate_path}")
96+ # print(f"Found duplicate array element at path: {duplicate_path}")
97+ # else:
98+ # seen_elements.append(item)
8199 if not isinstance (item , (list , tuple )):
82100 continue
83101 self .process_collection (item , base_path , f"{ array_path } [{ idx } ]" )
0 commit comments