Correcting order in which attributes are written to the JSON array to match the SQL query