fixed bugs in writing json to output