[utils] Make JSON file writes atomic (Fixes #3549)