Tidy up the populate/to_record{set} code for json a bit.

In the process fix a small bug.