Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dumping Json to file as array #738

Closed
ankitsultania opened this issue Sep 12, 2017 · 3 comments
Closed

Dumping Json to file as array #738

ankitsultania opened this issue Sep 12, 2017 · 3 comments

Comments

@ankitsultania
Copy link

Hi,

Is there a way to append a file with json objects as array one by one?

Any pointers are highly appreciated. I am stuck at doing this in a clean way.

@ankitsultania
Copy link
Author

ex:
json obj 1: { fname:x, lname:y}
write to file
json obj 2: { fname:a, lname:b}
write to file

file output:
[
{ fname:x, lname:y},
fname:a, lname:b}
]

@nlohmann
Copy link
Owner

As JSON requires opening and closing brackets as well as proper commas in between the elements, it is hard to achieve this. You may stream to a file and, after writing the last element, remove the last comma and add the closing bracket.

You may want to have a look at http://jsonlines.org/:

JSON Lines is a convenient format for storing structured data that may be processed one record at a time.

It is trivial to implement a JSON Lines writer and reader with this library.

@ankitsultania
Copy link
Author

Thanks for the response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants