Skip to content

Value function return empty object even if it exist #2086

Description

@xamix
  • Describe what you want to achieve.
    I need to get a default empty object in case an item do not exist in the specified json.

  • Describe what you tried.
    Using the function value.

  • Describe which system (OS, compiler) you are using.
    GCC 9 on Ubuntu x64.

  • Describe which version of the library you are using (release version, develop branch).
    I use develop branch.

Here is a simple example to demonstrate my question:

#include <iostream>

#include <nlohmann/json.hpp>
using json = nlohmann::json;

int main()
{
    json j = {
        {"what", "i'm here"}
    };

    json k = j.value("what", json::value_t::object);

    std::cout << "j:" << j.dump(4) << std::endl;
    std::cout << "k:" << k.dump(4) << std::endl;
    return 0;
}

This output k as empty even if the item exist:

j:{
    "what": "i'm here"
}
k:""

However it works (k is filled with the good value i'm here) if I change the call of value with one of the following:

json k = j.value<json>("what", json::value_t::object);
or
json k = j.value("what", json({}));

Is this the expected behavior?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions