Skip to content

Instantly share code, notes, and snippets.

@salmanx
Created March 25, 2023 17:27
Remove invalued values from hash and nested values from hash
def clean_json(hash)
invalid_values = ["N/A", "-", ""]
hash.each do |key, value|
if value.is_a?(Hash)
clean_json(value)
if value.values.any? { |v| invalid_values.include?(v) }
hash.delete(key)
end
elsif value.is_a?(Array)
value.each do |v|
if v.is_a?(Hash) || v.is_a?(Array)
clean_json(v)
end
end
if value.any? { |v| invalid_values.include?(v) }
hash.delete(key)
end
else
if invalid_values.include?(value)
hash.delete(key)
end
end
end
hash
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment