Skip to content

Instantly share code, notes, and snippets.

@cabo
Created March 10, 2023 09:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cabo/dafd9a841747de643c1daf04ca47494f to your computer and use it in GitHub Desktop.
Save cabo/dafd9a841747de643c1daf04ca47494f to your computer and use it in GitHub Desktop.
Diff two JSON files after normalizing them with jq
#!/usr/bin/env ruby
require 'open3'
require 'tempfile'
require 'shellwords'
if ARGV.size != 2
warn "** usage: jsondiff.rb j1 j2"
exit 63
end
files = ARGV.dup.map do |arg|
ARGV.replace([arg])
f = ARGF.read
result, err, _s = Open3.capture3("jq", stdin_data: f)
warn err
file = Tempfile.new("jsondiff-")
file.write(result)
file.close
file.path
end
exec("diff -u #{files.shelljoin}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment