Repository navigation
How to compare two JSON objects? #1007
Description
Activity
The
items()function replaces the deprecatediterator_wrapper.Can you provide an example so I can understand what you mean with "compare indexes"?
- addedstate: needs more infothe author of the issue needs to provide more detailsthe author of the issue needs to provide more details
on Mar 11, 2018 For example, i have this JSON string and parsing it. Let this JSON will be a
Settings.Settingsstring 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
DefaultSettingsi have three values, but inSettingsonly 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 useitems().The
updatefunction 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
Settingsis{"Value1": 100}andDefaultSettingslike above. Then the result would bes = {"Value1": 100, "Value2": true, "Value3": "Hello" }.- addedsolution: proposed fixa fix for the issue has been proposed and waits for confirmationa fix for the issue has been proposed and waits for confirmationand removedstate: needs more infothe author of the issue needs to provide more detailsthe author of the issue needs to provide more details
on Mar 11, 2018 Big thanks.
@zkelo FWIW: for a larger use cases you might consider using JSON-schemas for validating JSON objects.
Reacted by Eric Magnuson, Adrien M, Alex and Azeem
I have default app settngs in JSON object
DefaultSettingsand I'm parsing current app settings from fileConfig.json(ifstream). How to compare indexes fromSettingswith indexes inDefaultSettings? Does library have functions for this? I tried to compare withiterator_wrapperbut he's deprecated.