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

Best way to handle json files with missing key value pairs. #1893

Closed
shrikky opened this issue Jan 7, 2020 · 4 comments
Closed

Best way to handle json files with missing key value pairs. #1893

shrikky opened this issue Jan 7, 2020 · 4 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@shrikky
Copy link

shrikky commented Jan 7, 2020

There are two possible ways (maybe more that I missed) to check if key is present in the json.

Json J;
j.contains("mykey") == true?
auto it = j.find("mykey");

I have a test application where the configuraton properties (json key/val) may or may not be present as it is based on the test case. I don't want to do j.find or j.contains for every key before parsing it into the class. Is there a better way to ignore or return default value when a json key/val is not present in the json file?

Currently j.at("mykey").get_to(value) throws error if the mykey is not present in json.

@nlohmann
Copy link
Owner

nlohmann commented Jan 7, 2020

You may want to check the value function.

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jan 7, 2020
@shrikky
Copy link
Author

shrikky commented Jan 7, 2020

Brilliant ! thank you very much.

@nlohmann nlohmann closed this as completed Jan 7, 2020
@shrikky
Copy link
Author

shrikky commented Feb 11, 2020

Does Value work with loading an array objects into vectors? My vector always shows size 0 if I try something like std::vector setupTasks = j.value("setupTasks",setupTasks);

@nlohmann
Copy link
Owner

Yes, that should work.

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

No branches or pull requests

2 participants