bug fix: trailing comma in json when query hits limit in rest api

When a query hits the memory limit, there is a trailing comma because the while loop aborts early. This leads to invalid JSON, and things like the `json` python package will reject it.

The commit here just flips the order when breaking to output the next obj first, so that the final output avoids the trailing comma. Technically this will go even more over the memory limit - an alternative is to check earlier in the loop, before outputting the comma, but this seemed cleaner.

Let me know what you think?