Created
March 25, 2023 17:27
-
-
Save salmanx/1ef8e52e67fbfccb5ef515ca3ba3b4c1 to your computer and use it in GitHub Desktop.
Remove invalued values from hash and nested values from hash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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