The main  array was getting formatted in-place. Fixed it by creating a copy of the object.