You need to use create the json with
// ways to express the empty object {}
json empty_object_implicit = json({});
json empty_object_explicit = json::object();
b/c the code checks if it's a object in the function
ReturnType value(const typename object_t::key_type& key, ValueType && default_value) const
{
// value only works for objects
if (JSON_HEDLEY_LIKELY(is_object()))
{
// if key is found, return value and given default value otherwise
const auto it = find(key);
if (it != end())
{
return it->template get<ReturnType>();
}
return std::forward<ValueType>(default_value);
}
JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
}
https://github.com/nlohmann/json/blob/develop/include/nlohmann/json.hpp#L2281
The json can be a string, number, or array etc.