Normalize json domain for tree state

JSON string could be different for the same object because of spaces.

issue4027
review11331005
On restore_history select the record with highest __id

issue3861
review13391002