Skip to content

Instantly share code, notes, and snippets.

@ogijun
Last active November 26, 2024 09:16
Show Gist options
  • Save ogijun/be484e0e5c75ecca4ec400daad71b404 to your computer and use it in GitHub Desktop.
Save ogijun/be484e0e5c75ecca4ec400daad71b404 to your computer and use it in GitHub Desktop.
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