Skip to content

Instantly share code, notes, and snippets.

@glurp
Created October 2, 2019 16:59
Show Gist options
  • Save glurp/f37df8aab5d58c4cd77c4a5637a38fd9 to your computer and use it in GitHub Desktop.
Save glurp/f37df8aab5d58c4cd77c4a5637a38fd9 to your computer and use it in GitHub Desktop.
On json syntaxe error, pretty-print the fragment where the issue occure
#!/usr/bin/ruby
require 'json'
mess=File.read(ARGV.first).split(/\r?\n/).join("").gsub(/\s\s+/," ")
loop {
JSON.parse(mess) rescue break
exit(0)
}
loop {
a=mess[/({[^{]*?},?)/,1]
break unless a
JSON.parse(a.gsub(/,$/,"")) rescue puts("Error json #{$!} : #{$!.to_s.size<30 ? a : ''}")
mess=mess.gsub(a,'')
}
JSON.parse(mess.gsub(/,$/,"")) rescue puts("Error json #{$!}") if mess.strip.size>0
exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment