Search code examples
c++jsonrapidjson

iterate and retrieve nested object in JSON using rapidjson


I am parsing a JSON structure which is similar as follows

{
    "item1" : "value1"
    "item2" : "value2"
    // ...
    "itemn" : {
        "outernestedItem1" : {
            "innerNestedItem1" : "valuen1"
            "innerNestedItem2" : "valuen2"
        }
        // ....
        "outernestedItemn" : {
            "innerNestedItem1" : "valuen1"
            "innerNestedItem2" : "valuen2"
        }
    }
}

The number of outer nested items is not fixed, so I was iterating using iterator from rapidjson, inner-nested objects variables are fixed, so I can get access to them using [].

const rapidjson::Value& itemn = document["itemn"];
for (rapidjson::Value::ConstMemberIterator itr = itemn.MemberBegin();
itr != itemn.MemberEnd(); ++itr)
{
    rapidjson::StringBuffer sb;
    rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
    itr->value.Accept(writer);

    std::cout << sb["innerNestedItem1"].GetString();
    std::cout << sb["innerNestedItem2"].GetString();
}

but [] is not allowed with sb(string buffer), any idea how can I do this?

Edit1: I did it in very inefficient way, but just sharing the solution, so it might help someone to come up with efficient solution.

const rapidjson::Value& itemn = document["itemn"];
for (rapidjson::Value::ConstMemberIterator itr = itemn.MemberBegin();
itr != itemn.MemberEnd(); ++itr)
{
    rapidjson::StringBuffer sb;
    rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
    itr->value.Accept(writer);

    //changed from here onwards
    rapidjson::Document for_outer_nested_item;
    std::string temp = sb.GetString();
    char buffer2[100000];
    strcpy_s(buffer2, temp.c_str());
    for_outer_nested_item.ParseInsitu(buffer2);
    std::cout << executive_command["innerNestedItem1"].GetString() << std::endl;
    std::cout << executive_command["innerNestedItem2"].GetString() << std::endl;
}

Solution

  • First, let me provide credit to MiloYip at this link

    Second-- here's what I did for my project:

    rapidjson::Document document;
    // document holds a json document retrieved from a http GET request
    // I did not include all of that in this example.  I am only showing
    // the part of iterating through a nested object and retrieving members.
    
    std::vector<std::string> symbols;
    // holds the values I retrieve from the json document
    
    if (document.Parse<0>( symbol.c_str() ).HasParseError() )
        Log() << "ERROR: encountered a JSON parsing error" << std::endl;
    else {
        // Get the nested object that contains the elements I want.
        // In my case, the nested object in my json document was results
        // and the values I was after were identified as "t"
        rapidjson::Value& results = document["results"];
        assert(results.IsArray());
        for (rapidjson::SizeType i = 0; i < results.Size(); i++) {
            // Store the value of the element in a vector.
            symbols.emplace_back(results[i]["t"].GetString());
    }                            
    

    I think this is a pretty clean/efficient approach.