-
-
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
Prevent Null values to appear in .dump() #995
Comments
No, this is currently not possible. If the value is coming from an external JSON file, then you could add a callback to the parser which removes the |
Ok. Thank you for the help. |
Solution: std::string a = jsonprop.dump();
jsonprop = nlohmann::json::parse(a, RemoveNullCallBack);
nlohmann::json::parser_callback_t RemoveNullCallBack = [](int depth, nlohmann::json::parse_event_t event, nlohmann::json & parsed){
if (event == nlohmann::json::parse_event_t::value and parsed == nullptr)
{
return false;
}
else
{
return true;
}
}; Not optimal but works for now. |
Thanks for checking back! If your workflow is to first parse and then dump, then I think this is the best you can get, because you filter right during parsing and never need to take care about |
|
@nlohmann FYI. Had to clean null values recently and found nothing online. A simple DFS did the trick. |
This code didnt work for array indexes, solution:
|
Imagine that you have the following json string created by .dump()
{"connectorId":0, "meterValue": [{"sampledValue":[{"context":"Sample.Periodic","format":null,"location":"Body","measurand":null,"phase":"L1","unit":"V","value":"18440"},{"context":"Sample.Periodic","format":null,"location":"Body","measurand":null,"phase":"L2","unit":"V","value":"46927"},{"context":"Sample.Periodic","format":null,"location":"Body","measurand":null,"phase":"L3","unit":"V","value":"44515"}]
How can I configure the library to not include null values in the dump? I research in the documentation by with no success. The only way I can find is to create a For loop that removes all null entries but is quite inefficient.
Thank you for the help,
Nuno
The text was updated successfully, but these errors were encountered: