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 merge (and not replace) different Json::Value objects in jsoncpp #2001

Closed
ashishsingh5 opened this issue Mar 20, 2020 · 1 comment
Closed
Labels
kind: question solution: invalid the issue is not related to the library

Comments

@ashishsingh5
Copy link

ashishsingh5 commented Mar 20, 2020

I have JSON config in Json::Value json_config of following format:

{
   "configuration":{
      "services":{
         "analytics":{
            "export-profile":[
               {
                  "name":"export_1000",
                  "transport":"grpc"
               }
            ],
            "sensor":[
               {
                  "name":"sensor_1000",
                  "export-name":"export_1000",
               }
            ]
         }
      }
   }
} 

How can I merge multiple similar above mentioned Json::Value such as below

{
   "configuration":{
      "services":{
         "analytics":{
            "export-profile":[
               {
                  "name":"export_1001",
                  "transport":"grpc"
               }
            ],
            "sensor":[
               {
                  "name":"sensor_1001",
                  "export-name":"export_1001",
               }
            ]
         }
      }
   }
} 

into one so that the final config looks something like below:

{
   "configuration":{
      "services":{
         "analytics":{
            "export-profile":[
               {
                  "name":"export_1000",
                  "transport":"grpc"
               },
               {
                  "name":"export_1001",
                  "transport":"grpc"
               },

               ...
            ],
            "sensor":[
               {
                  "name":"sensor_1000",
                  "export-name":"export_1000",
               },
               {
                  "name":"sensor_1001",
                  "export-name":"export_1001",
               },

               ...
            ]
         }
      }
   }
} 

I tried below, but it ends up replacing the previous config, and at the end only the last one remains.

void merge_json_objs (Json::Value& super, Json::Value& sub)
{
    // Merge all members of sub into super
    for (const auto& key : sub.getMemberNames()) {
        if (super[key].type() == Json::objectValue &&
            sub[key].type() == Json::objectValue) {
            merge_json_objects(super[key], sub[key]);
        } else {
            super[key] = sub[key];
        }
    }
}

OS: FreeBSD11,
compiler: gcc 4.2
JsonCpp version: 1.7.4

@nlohmann
Copy link
Owner

This is the wrong repository - this is "JSON for Modern C++", not JsonCpp.

@nlohmann nlohmann added the solution: invalid the issue is not related to the library label Mar 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: invalid the issue is not related to the library
Projects
None yet
Development

No branches or pull requests

2 participants