Retry json file writing 3 times before failing, fixes #2286