detectDuplicateKeys static method
Detects duplicate keys in raw JSON string content.
Returns a list of duplicate key paths found.
Implementation
static List<String> detectDuplicateKeys(String content) {
final duplicates = <String>[];
final lines = content.split('\n');
final keyStack = <String>[];
final keyCounts = <String, int>{};
for (final line in lines) {
final trimmed = line.trim();
final keyMatch = RegExp(r'^"([^"]+)"\s*:').firstMatch(trimmed);
if (keyMatch != null) {
final key = keyMatch.group(1)!;
keyStack.add(key);
final fullPath = keyStack.join('.');
keyCounts[fullPath] = (keyCounts[fullPath] ?? 0) + 1;
}
if (trimmed == '}' || trimmed.endsWith('},')) {
if (keyStack.isNotEmpty) keyStack.removeLast();
}
}
for (final entry in keyCounts.entries) {
if (entry.value > 1) {
duplicates.add(entry.key);
}
}
return duplicates;
}