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

How to compare two JSON objects? #1007

Closed
zkelo opened this issue Mar 11, 2018 · 5 comments
Closed

How to compare two JSON objects? #1007

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

Comments

@zkelo
Copy link

zkelo commented Mar 11, 2018

I have default app settngs in JSON object DefaultSettings and I'm parsing current app settings from file Config.json (ifstream). How to compare indexes from Settings with indexes in DefaultSettings? Does library have functions for this? I tried to compare with iterator_wrapper but he's deprecated.

@nlohmann
Copy link
Owner

The items() function replaces the deprecated iterator_wrapper.

Can you provide an example so I can understand what you mean with "compare indexes"?

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Mar 11, 2018
@zkelo
Copy link
Author

zkelo commented Mar 11, 2018

For example, i have this JSON string and parsing it. Let this JSON will be a Settings. Settings string I'm getting from file.
{ "Value1": 4, "Value3": "Hello" }

And this string will be DefaultSettings.
{ "Value1": 4, "Value2": true, "Value3": "Hello" }

This string I'm parsing into objects. In DefaultSettings i have three values, but in Settings only two. And when I will use Settings["Value2"] it will generate an exception. For fix it, I want to compare indexes and their values after parsing. Now I'll try to use items().

@nlohmann
Copy link
Owner

The update function could be handy for you:

// start with a copy of the default settings
json s = DefaultSettings;
// overwrite all default settings with those given in Settings
s.update(Settings);

Assume Settings is {"Value1": 100} and DefaultSettings like above. Then the result would be s = {"Value1": 100, "Value2": true, "Value3": "Hello" }.

@nlohmann nlohmann added solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed state: needs more info the author of the issue needs to provide more details labels Mar 11, 2018
@zkelo
Copy link
Author

zkelo commented Mar 11, 2018

Big thanks.

@zkelo zkelo closed this as completed Mar 11, 2018
@pboettch
Copy link
Contributor

@zkelo FWIW: for a larger use cases you might consider using JSON-schemas for validating JSON objects.

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

3 participants