Home > Net >  create json object for vector<bool> using nlohman json
create json object for vector<bool> using nlohman json

Time:04-26

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]
}

CodePudding user response:

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]
}
  • Related