Search code examples
c++jsonnlohmann-json

Convert vector of integers to string using nlohman json


I am trying to convert an std::vector of integers. For this task I am using nlohmann library. What I am expecting to get is the string "[3,5,18,11]", but I am receiving "[[3,5,18,11]]" as a result. I do not understand why I am getting another array wrapped and how to avoid it? Will appreciate any help, thanks in advance.

#include <iostream>
#include "nlohmann/json.hpp"

using namespace nlohmann;

int main() {
    const std::vector<uint64_t> data = { 3, 5, 18, 11 };

    json j = json{ data };
    auto result = j.dump();
    std::cout << result << std::endl;

    return 0;
}

Solution

  • This is just enough:

    const std::vector<uint64_t> data = { 3, 5, 18, 11 };
    json j = data;