Improved table_to_json() validity checks