Because the document is initialized currently in L53 and in L59 json.Unmarshal is performed and as per Go docs;
To unmarshal a JSON object into a map, Unmarshal first establishes a map to use. If the map is nil, Unmarshal allocates a new map. Otherwise Unmarshal reuses the existing map, keeping existing entries.
Source
So old data is retained, and this leads to the CSV export being incorrect under certain situations, f.ex. if new document doesn't override all the fields.
To fix, re-initialize document inside the loop rather than outside.
Because the
document
is initialized currently in L53 and in L59json.Unmarshal
is performed and as per Go docs;So old data is retained, and this leads to the CSV export being incorrect under certain situations, f.ex. if new document doesn't override all the fields.
To fix, re-initialize document inside the loop rather than outside.