create json object for vector<bool> using nlohman json

483 Views Asked by At

I want to convert a vector<bool> vec = {true, false,true} to json object using nlohman json lib to send through restapi.

I expect the converted json object in the form

{
  "data" : [true, false, true]
}
1

There are 1 best solutions below

0
On BEST ANSWER

To do this in nlohmann/json, all that needs doing is creating an empty nlohmann JSON object and assigning the boolean vector to a field, "data".

e.g.

std::vector<bool> vec = {true, false, true};

nlohmann::json j;
j["data"] = vec;

yields the json object

{
    "data":[true,false,true]
}