Skip to content

Instantly share code, notes, and snippets.

@dohzya
Last active December 5, 2016 08:52
Show Gist options
  • Save dohzya/fb6bd065775ca919b0408d52661925f1 to your computer and use it in GitHub Desktop.
Save dohzya/fb6bd065775ca919b0408d52661925f1 to your computer and use it in GitHub Desktop.
Sort JSON content (for easy comparison)
#!/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