I have an application which, from time to time, saves object arrays into text files. All the elements of the object array display identical structure.
As an example, consider the following object array:
[{"A":1,"B":2},{"A":11,"B":22},{"A":111,"B":222}]
The need is to save this into a file such that it would look like this:
[{"A":1,"B":2} ,
{"A":11,"B":22} ,
{"A":111,"B":222}]
meaning, one record within the text file for each object in the array.
The reason for that is that those arrays may include hundreds and up to several thousands of elements and I wish to keep these files READABLE for humans.
Is there any trick that can be invoked when using the standard JSON.stringify method? If not, any other suggestion will be appreciated.
2
Answers
While this will not give the exact result you’re looking for (breaking the line for individual array elements, rather than for the whole JSON), consider taking a look into the
space
parameter of theJSON.stringify
method, which will add indentation and spacing and perhaps, will store the JSON in a more human-readable form.Just stringify individual items.
You could use
map().join()
but it’s a bit slower.