If you are dealing with an unstable API, you may want to iterate through all the keys to check for null. This will prevent crash when saving the object as well.
+ (id)replaceNullWithEmptyStringForObject:(id)object {
if ([object isKindOfClass:[NSArray class]]) {
NSMutableArray *mutableArray = [object mutableCopy];
[mutableArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[mutableArray setObject:[self replaceNullWithEmptyStringForObject:obj] atIndexedSubscript:idx];
}];
return mutableArray;
} else {
NSMutableDictionary *mutableDictionary = [object mutableCopy];
[mutableDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
if ([value isKindOfClass: [NSMutableDictionary class]]) {
[self replaceNullWithEmptyStringForObject:value];
} else if ([value isKindOfClass:[NSArray class]]) {
[mutableDictionary setObject:[self replaceNullWithEmptyStringForObject:value] forKey:key];
} else if ([value isKindOfClass:[NSNull class]]) {
[mutableDictionary setValue:@"" forKey:key];
}
}];
return mutableDictionary;
}
}
Tested: