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

Prevent Null values to appear in .dump() #995

Closed
nuno407 opened this issue Mar 5, 2018 · 8 comments
Closed

Prevent Null values to appear in .dump() #995

nuno407 opened this issue Mar 5, 2018 · 8 comments
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@nuno407
Copy link

nuno407 commented Mar 5, 2018

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

@nlohmann
Copy link
Owner

nlohmann commented Mar 6, 2018

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 null entries.

@nuno407
Copy link
Author

nuno407 commented Mar 6, 2018

Ok. Thank you for the help.
I will check the callbacks

@nuno407
Copy link
Author

nuno407 commented Mar 6, 2018

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.
Thank you.

@nlohmann
Copy link
Owner

nlohmann commented Mar 6, 2018

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 null again later.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Mar 6, 2018
@nlohmann nlohmann closed this as completed Mar 6, 2018
@albertborges
Copy link

albertborges commented Jul 14, 2020

static void recurseAndFilterNullValues(nlohmann::json & jsonObject) {
    if (!jsonObject.is_object() && !jsonObject.is_array()) {
        return;
    }

    std::vector<nlohmann::json::object_t::key_type> keysToRemove;
    for (auto &it : jsonObject.items()) {
        if (it.value().is_null())
        {
            keysToRemove.push_back(it.key());
            continue;
        }
        
        recurseAndFilterNullValues(it.value());
    }

    for (auto key : keysToRemove)
    {
        jsonObject.erase(key);
    }
}

@albertborges
Copy link

@nlohmann FYI. Had to clean null values recently and found nothing online. A simple DFS did the trick.

@deleeuwict
Copy link

static void recurseAndFilterNullValues(nlohmann::json & jsonObject) {
    if (!jsonObject.is_object() && !jsonObject.is_array()) {
        return;
    }

    std::vector<nlohmann::json::object_t::key_type> keysToRemove;
    for (auto &it : jsonObject.items()) {
        if (it.value().is_null())
        {
            keysToRemove.push_back(it.key());
            continue;
        }
        
        recurseAndFilterNullValues(it.value());
    }

    for (auto key : keysToRemove)
    {
        jsonObject.erase(key);
    }
}

This code didnt work for array indexes, solution:

void RecurseAndFilterNullValues(nlohmann::json& jsonObject) 
{
	if (!jsonObject.is_object() && !jsonObject.is_array()) {
		return;
	}

	std::vector<nlohmann::json::object_t::key_type> keysToRemove;
	for (auto& it : jsonObject.items()) {
		if (it.value().is_null())
		{
			keysToRemove.push_back(it.key());
			continue;
		}

		RecurseAndFilterNullValues(it.value());
	}

	for (auto const& key : keysToRemove)
	{
		if (jsonObject.is_object())
		{
			jsonObject.erase(key);
		}
		else
		{
			assert(jsonObject.is_array());
			jsonObject.erase(static_cast<size_t>(std::stoi(key)));
			//other indexes are now invalid
			return RecurseAndFilterNullValues(jsonObject);
		}
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

4 participants