remove trailing whitespace
even smarter JSON detection