Skip to content

contains() doesn't find fields with null value #4381

Answered by nlohmann
bbalaban asked this question in Q&A
Discussion options

You must be logged in to vote

Your code is odd. contains returns true if the given key is present in the object - regardless of the value.

This code works for me:

#include <nlohmann/json.hpp>

using json = nlohmann::json;

int main()
{
    auto j = json::parse("{\"firstName\": \"Bellina\",\"company\" : null, \"address1\" : \"158 Rusk Terrace\"}");
    assert(j.contains("company"));
    assert(j.contains("address1"));
}

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@bbalaban
Comment options

@nlohmann
Comment options

@bbalaban
Comment options

@nlohmann
Comment options

Answer selected by nlohmann
@bbalaban
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants