Last active
November 26, 2024 09:16
-
-
Save ogijun/be484e0e5c75ecca4ec400daad71b404 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'json' | |
def d2j(data) | |
case data | |
when Hash | |
if data.keys.length == 1 && %w[S N M L NULL BOOL].include?(data.keys.first) | |
key, value = data.first | |
case key | |
when 'S', 'N' | |
value.is_a?(String) && key == 'N' ? value.to_i : value | |
when 'NULL' | |
nil | |
when 'BOOL' | |
value | |
when 'M' | |
d2j(value) | |
when 'L' | |
value.map { |v| d2j(v) } | |
else | |
value | |
end | |
else | |
data.transform_values { |v| d2j(v) } | |
end | |
when Array | |
data.map { |v| d2j(v) } | |
else | |
data | |
end | |
end | |
if __FILE__ == $0 | |
input_data = if ARGV[0] | |
File.read(ARGV[0]) | |
else | |
STDIN.read | |
end | |
dynamodb_data = JSON.parse(input_data) | |
standard_json = d2j(dynamodb_data["Item"]) | |
puts JSON.pretty_generate(standard_json, indent: ' ') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment