Improved json encoding for saving data