-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
How to solve large json file? #927
Comments
Does this error message occur during parsing? |
Yes~. Code is simple. std::fstream read file, pass data to json obj. |
Right now, we only support DOM-like parsing to memory. There is an experimental SAX-like approach, but it has not been merged yet. With such an approach, you may parse and process the input without the need of converting each element to a JSON value and storing it. Would it be possible to share your input so we could experiment whether the new approach would help? |
Ok, Laterly I will upload data to Google Drive and paste link here. Finally I choose sed and grep to parse my data. |
Thanks! |
Thanks. What did you want to do with the file after parsing? I am asking because it is possible to define a callback function to be called during parsing, see https://nlohmann.github.io/json/classnlohmann_1_1basic__json_ab4f78c5f9fd25172eeec84482e03f5b7.html#ab4f78c5f9fd25172eeec84482e03f5b7 With such a function, you can process the input during parsing without the need of actually storing all input data. |
Example: #include <iostream>
#include <fstream>
#include "json.hpp"
using json = nlohmann::json;
int main()
{
std::size_t count = 0;
auto x = [&count](int depth, json::parse_event_t event, json& parsed) {
if (event == json::parse_event_t::object_end)
{
++count;
return false; // do not store the object value
}
else
{
return true;
}
};
std::ifstream f("wk_file_list.json");
json::parse(f, x);
std::cerr << "file has " << count << " elements\n";
} Output: On my machine, this takes less than 3 MB of memory. |
Hi @nlohmann , |
The code above is just an example how to cope with a single array of objects. You may need to adjust it to your needs. Please also have a look at the documentation. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
A small update on this (as it was mentioned in #971):
|
My json file has a array of approximately ten million data.
ucrtbased.dll!0f5160d0() Unknown
[Frames below may be incorrect and/or missing, no symbols loaded for ucrtbased.dll]
[External Code]
The text was updated successfully, but these errors were encountered: