Converting a Json::Value to std::string?

You can use a Json::Writer to do exactly this, since I assume you want to save it somewhere so you don't want human readable output, your best bet would be to use a Json::FastWriter and then you can call the write method with the parameter of your Json::Value (ie. your root) and then that simply returns a std::string like so:

Json::FastWriter fastWriter;
std::string output = fastWriter.write(root);

Json::Writer is deprecated and Json::StreamWriterBuilder should instead be used, as stated in the documentation of Json::Writer.

Json::writeString writes into a stringstream and then returns a string:

Json::Value json = ...;
Json::StreamWriterBuilder builder;
builder["indentation"] = ""; // If you want whitespace-less output
const std::string output = Json::writeString(builder, json);

Kudos to cdunn2001's answer here: How to get JsonCPP values as strings?


You can also use the method toStyledString().

jsonValue.toStyledString();

The method "toStyledString()" converts any value to a formatted string. See also the link: doc for toStyledString