Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to check if key existed #1000

Closed
leeqicheng opened this issue Mar 9, 2018 · 11 comments
Closed

How to check if key existed #1000

leeqicheng opened this issue Mar 9, 2018 · 11 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@leeqicheng
Copy link

Hi to all is there a method to check if the key exist ,

what is the close to

Java JSON

indivFB.has("object_id")

Thanks

@nlohmann
Copy link
Owner

nlohmann commented Mar 9, 2018

Just like for a map: find().

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Mar 9, 2018
@leeqicheng
Copy link
Author

Hi thanks for the reply but im using iterator

any idea why is causing this problem ?

Value of type 'iterator' (aka 'nlohmann::detail::iter_impl<nlohmann::basic_json<std::map, std::vector, std::__1::basic_string, bool, long long, unsigned long long, double, std::allocator, adl_serializer> >') is not contextually convertible to 'bool'

@nlohmann
Copy link
Owner

nlohmann commented Mar 9, 2018

I would need to see a code example for that.

@leeqicheng
Copy link
Author

Hi thanks for time,

The problem i face is that sometimes the json result doesnt have the key so im trying to check for the key

screen shot 2018-03-09 at 5 22 49 pm

@nlohmann
Copy link
Owner

nlohmann commented Mar 9, 2018

When you iterate over an object (which I assume - I have not seen the JSON), doing this with a range for only gives you access to the values, not to the keys. You need to use items() for this.

The exception comes, because you try to assign a number (from ...["total_count"]) to a string (totallike). Try to output ...["total_count"] directly and use a number type for totallike.

@leeqicheng
Copy link
Author

Sorry wrong image it should be this

screen shot 2018-03-09 at 5 27 34 pm

@nlohmann
Copy link
Owner

nlohmann commented Mar 9, 2018

Then use find:

if (json_field_data[p]["reactions"]["summary"].find("total_count") != json_field_data[p]["reactions"]["summary"].end())
   // now you can be sure json_field_data[p]["reactions"]["summary"]["total_count"] exists

Alternatively, you could use value and provide a default value (e.g., 0):

totallike = json_field_data[p]["reactions"]["summary"].value("total_count", 0);

@leeqicheng
Copy link
Author

Thanks for the library and the help

@Tudyx
Copy link

Tudyx commented Jan 13, 2021

In case if we are not sure if the keys before the nested key exist, should we use a nested if ? Because in the documentation it's wrote that is an undefined behavior if the element with key idx accessing by operator[] does not exist.

if (json_field_data[p].find("reactions") != json_field_data[p].end()){
    if(json_field_data[p]["reactions"].find("summary") != json_field_data[p]["reactions"].end()){
        if(json_field_data[p]["reactions"]["summary"].find("total_count") != json_field_data[p]["reactions"]["summary"].end())
    }
}

@nlohmann
Copy link
Owner

Yes, you need to check for each level. Note there is a contains function that could simplify your code: https://json.nlohmann.me/api/basic_json/contains/

Also note that that JSON Pointer could help you, see #2585 (comment)

@Tudyx
Copy link

Tudyx commented Jan 20, 2021

Thank you! I chose a JSON Pointer option like in this comment #1449 (comment)
If it can be useful for someone :

if( json_field_data[p].contains(json::json_pointer("/reactions/summary/total_count")) == true ){

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants