Search code examples
c++rapidjson

Get nested JSON member without getting each intermediate object directly


I am processing a 2MB JSON string in C++ using RapidJSON. I know you can try getting an object from the current level by doing:

rapidjson::Value& json = document["object1"];

But, is there a way to get something that's several levels deep in one go, without having to get each (and possibly irrelevant to my end goal) intermediate object along the way? Something along the conceptual lines of:

rapidjson::Value& member = document["object1.object2.object3.member"];

I've searched for this and haven't come up with anything, so I'm led to believe the answer is no, but I wanted to be sure.


Solution

  • Use RapidJSON's JSON pointer API:

    #include "rapidjson/pointer.h"
    
    Document document;
    if (Value* member = GetValueByPointer(document, "/object1/object2/object3/member")) {
        // the pointer was sucessfully resolved in the document
        // do something with member
    }