1# <small>nlohmann::basic_json::</small>contains 2 3```cpp 4// (1) 5bool contains(const typename object_t::key_type& key) const; 6 7// (2) 8template<typename KeyType> 9bool contains(KeyType&& key) const; 10 11// (3) 12bool contains(const json_pointer& ptr) const; 13``` 14 151. Check whether an element exists in a JSON object with a key equivalent to `key`. If the element is not found or the 16 JSON value is not an object, `#!cpp false` is returned. 172. See 1. This overload is only available if `KeyType` is comparable with `#!cpp typename object_t::key_type` and 18 `#!cpp typename object_comparator_t::is_transparent` denotes a type. 193. Check whether the given JSON pointer `ptr` can be resolved in the current JSON value. 20 21## Template parameters 22 23`KeyType` 24: A type for an object key other than [`json_pointer`](../json_pointer/index.md) that is comparable with 25 [`string_t`](string_t.md) using [`object_comparator_t`](object_comparator_t.md). 26 This can also be a string view (C++17). 27 28## Parameters 29 30`key` (in) 31: key value to check its existence. 32 33`ptr` (in) 34: JSON pointer to check its existence. 35 36## Return value 37 381. `#!cpp true` if an element with specified `key` exists. If no such element with such key is found or the JSON value 39 is not an object, `#!cpp false` is returned. 402. See 1. 413. `#!cpp true` if the JSON pointer can be resolved to a stored value, `#!cpp false` otherwise. 42 43## Exception safety 44 45Strong exception safety: if an exception occurs, the original value stays intact. 46 47## Exceptions 48 491. The function does not throw exceptions. 502. The function does not throw exceptions. 513. The function can throw the following exceptions: 52 - Throws [`parse_error.106`](../../home/exceptions.md#jsonexceptionparse_error106) if an array index begins with 53 `0`. 54 - Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) if an array index was not a 55 number. 56 57## Complexity 58 59Logarithmic in the size of the JSON object. 60 61## Notes 62 63- This method always returns `#!cpp false` when executed on a JSON type that is not an object. 64- This method can be executed on any JSON value type. 65 66!!! info "Postconditions" 67 68 If `#!cpp j.contains(x)` returns `#!c true` for a key or JSON pointer `x`, then it is safe to call `j[x]`. 69 70## Examples 71 72??? example "Example: (1) check with key" 73 74 The example shows how `contains()` is used. 75 76 ```cpp 77 --8<-- "examples/contains__object_t_key_type.cpp" 78 ``` 79 80 Output: 81 82 ```json 83 --8<-- "examples/contains__object_t_key_type.output" 84 ``` 85 86??? example "Example: (2) check with key using string_view" 87 88 The example shows how `contains()` is used. 89 90 ```cpp 91 --8<-- "examples/contains__keytype.c++17.cpp" 92 ``` 93 94 Output: 95 96 ```json 97 --8<-- "examples/contains__keytype.c++17.output" 98 ``` 99 100??? example "Example: (3) check with JSON pointer" 101 102 The example shows how `contains()` is used. 103 104 ```cpp 105 --8<-- "examples/contains__json_pointer.cpp" 106 ``` 107 108 Output: 109 110 ```json 111 --8<-- "examples/contains__json_pointer.output" 112 ``` 113 114## Version history 115 1161. Added in version 3.11.0. 1172. Added in version 3.6.0. Extended template `KeyType` to support comparable types in version 3.11.0. 1183. Added in version 3.7.0. 119