11import json
2- from typing import Dict , List , Set , Tuple
2+ from collections import OrderedDict
3+ from typing import Dict , List , Tuple
34
45JsonPrimitive = str | int | float | bool | None
56type JsonValue = JsonPrimitive | JsonObject | JsonArray
@@ -19,8 +20,8 @@ class DuplicateKeyChecker:
1920 """
2021
2122 def __init__ (self ):
22- self . duplicate_keys : Set [ str ] = set ()
23- self .duplicate_paths : Set [str ] = set ()
23+ # Here a list of paths because the same key name could be at different levels
24+ self .duplicate_keys_and_paths : OrderedDict [str , list [ str ]] = OrderedDict ()
2425 # Track keys at each path level to detect duplicates
2526 self .key_registry : Dict [str , Dict [str , bool ]] = {}
2627 self .current_duplicate_index : Dict [str , int ] = {}
@@ -47,8 +48,8 @@ def check_key(self, key: str, path: List[str]) -> None:
4748 current_level = "." .join (path )
4849 current_keys = self .key_registry .setdefault (current_level , {})
4950 if key in current_keys :
50- self . duplicate_keys . add ( key )
51- self .duplicate_paths . add ( "." . join ( path + [ key ]) )
51+ duplicate_path = "." . join ( path + [ key ] )
52+ self .duplicate_keys_and_paths . setdefault ( key , []). append ( duplicate_path )
5253 print (f"Found duplicate key: { key } at path: { '.' .join (path + [key ])} " )
5354 else :
5455 current_keys [key ] = True
@@ -101,8 +102,9 @@ def check_duplicate_keys(json_content: str) -> Tuple[List[str], List[str]]:
101102 checker = DuplicateKeyChecker ()
102103 checker .traverse_json (parsed_data , ["root" ])
103104
104- duplicates = list (checker .duplicate_keys )
105- paths = list (checker .duplicate_paths )
105+ duplicates = list (checker .duplicate_keys_and_paths .keys ())
106+ # flatten the list of paths
107+ paths = sum (checker .duplicate_keys_and_paths .values (), [])
106108 print ("Final duplicates:" , duplicates )
107109 print ("Final paths:" , paths )
108110
0 commit comments