Skip to content

How to compare two JSON objects? #1007

Description

@zkelo

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.

Activity

  1. nlohmann commented on Mar 11, 2018

    @nlohmann
    Owner

    The items() function replaces the deprecated iterator_wrapper.

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

  2. zkelo commented on Mar 11, 2018

    @zkelo
    Author

    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().

  3. nlohmann commented on Mar 11, 2018

    @nlohmann
    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" }.

  4. added
    solution: proposed fixa fix for the issue has been proposed and waits for confirmation
    and removed
    state: needs more infothe author of the issue needs to provide more details
    on Mar 11, 2018
  5. zkelo commented on Mar 11, 2018

    @zkelo
    Author

    Big thanks.

  6. pboettch commented on Mar 11, 2018

    @pboettch
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    solution: proposed fixa fix for the issue has been proposed and waits for confirmation

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions