Json data creation for nlohmann Json data creation for nlohmann json json

Json data creation for nlohmann


You could wrap the JSON data in a raw string literal and use the _json user-defined literal to parse it:

json v2 = R"({"BEGIN": "200", "END": "300"})"_json;

Or you could make it directly (without parsing), but using valid C++ syntax:

json v2 = {{"BEGIN", "200"}, {"END", "300"}};


analyze pretty much what you want to do:

this here:

{"BEGIN":"0","END":"100","MIDDLE":50}

is an object at index 0 in the hierarchy:

globalData.first

so you can do get the element at index 0 in the array and add a new key:value

std::string st = "{\"GLOBAL DATA\":{\"FIRST\": [{\"BEGIN\": \"0\", \"END\" : \"100\"}],\"SECOND\":\"SomeData\",\"THIRD\":\"SomeMoreData\"}}";nlohmann::json second = nlohmann::json::parse(st);second["GLOBAL DATA"]["FIRST"].at(0).push_back({"MIDDLE", 50});std::cout << second.dump().c_str();

the output:

{"GLOBAL DATA":{"FIRST": [{"BEGIN":"0","END":"100","MIDDLE":50}],"SECOND":"SomeData","THIRD":"SomeMoreData"}}