Last active
December 5, 2016 08:52
-
-
Save dohzya/fb6bd065775ca919b0408d52661925f1 to your computer and use it in GitHub Desktop.
Sort JSON content (for easy comparison)
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
#!/usr/bin/env ruby | |
require 'json' | |
def sort_json(json) | |
case json | |
when Hash; Hash[json.map { |k, v| [k, sort_json(v)] }.sort_by { |k| k }] | |
when Array; json.map { |v| sort_json(v) }.sort_by{ |v| v.to_json } | |
else json | |
end | |
end | |
puts sort_json(JSON.parse(STDIN.read, max_nesting: 1000)).to_json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment