Fix trailing comma json bug in rest api

When a query hits the memory limit, there is a trailing comma because the while loop aborts early.
This commit will output the next obj before breaking, so that the final output is valid JSON.