Fixed the resetting of the json file on removal failures

Closes #3329

Overwriting the ``$json`` variable with a different object was creating this bug