Added function for rewriting back to JSON. Very messy and needs recursion to hit all depths of whatever might be passed in, but it works.