Попробуйте это:
func flatten(dictionary: [String: Any]) -> [String: Any] {
func flattenRec(output: inout [String: Any], keyPath: String, value: Any) {
if value is String {
output[keyPath] = value
}
if let dict = value as? [String: Any] {
dict.forEach { key, value in
flattenRec(output: &output, keyPath: "\(keyPath).\(key)", value: value)
}
}
}
var outputDict = [String: Any]()
dictionary1.forEach { key, value in
flattenRec(output: &outputDict, keyPath: key, value: value)
}
return outputDict
}
let output = flatten(dictionary: dictionary1)