2

I have a list of paths like:"blabla/bleble/blibli", "blabla/blibli/bleble" I've succeeded to transform one to this: {"blabla":{"bleble":{"blibli":null}}} with this function:

nlohmann::json transform(std::pair<std::string, int> aaa){
    nlohmann::json pathJsoned = {};
    auto splitedPath = split(aaa.first, '/');
    nlohmann::json* bla = &pathJsoned;

    for(int i = 0; i < splitedPath.size(); i++){
        if( !bla->is_null() && bla->at(splitedPath[i].c_str()).is_object() ){
            if(i == splitedPath.size()-1){
                std::cout << i << " || " << splitedPath.size() << std::endl;
                pathJsoned[splitedPath[i].c_str()] = "FILE";
            }else{
                std::cout << i << " || " << splitedPath.size() << std::endl;
                pathJsoned[splitedPath[i].c_str()] = nlohmann::json();
            }
        }
        nlohmann::json* a = &bla->operator[](splitedPath[i].c_str());
        bla = a;
    }
    return pathJsoned;
}

But now i want to merge it and it doesn't work. I've tried:

nlohmann::json merge( const nlohmann::json &a, const nlohmann::json &b )
{
    nlohmann::json result = a.flatten();
    nlohmann::json tmp = b.flatten();
    for ( auto it = tmp.begin(); it != tmp.end(); ++it )
        result[it.key()] = it.value();
    return result.unflatten();
}

The result is useless, it's like the merge doesn't do anything, I've tried the merge_patch method of nlohmann::json library but it do half of the job:

{
    "blabla": {
        "bleble": {
            "blibli": null
        },
        "blibli": {}
    }
}

The expected result:

{
    "blabla": {
        "bleble": {
            "blibli": null
        },
        "blibli": {
            "bleble": null
        }
    }
}

Do you know if it's possible to merge the complete json ?

here is the whole code: https://pastebin.com/Qyc8EQN0

0 Answers0