Files
json/docs/mkdocs/docs/api/basic_json_view/contains.md
Niels Lohmann 2f087c6b73 Document element access, lookup, and iteration of json_view
- API pages for operator[], at, front, back, find, contains, count,
  begin, end, cbegin, cend, items, and type_name of basic_json_view,
  linked both ways with the basic_json pages
- the feature page and size() describe document order and duplicate
  keys
- the examples show when the view helps: reading a few fields of a large
  text, probing optional members, and members in source order

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-09-30 15:08:00 +02:00

1.7 KiB

nlohmann::basic_json_view::contains

bool contains(string_view_t key) const;
bool contains(const char* key) const;
bool contains(const string_t& key) const;

Checks whether the value is an object with a member with key key.

Parameters

key (in)
key value to check its existence

Return value

#!cpp true if the value is an object and has a member with key key, #!cpp false otherwise.

Exception safety

No-throw guarantee: this function never throws exceptions.

Complexity

Linear in the number of members: as for ordered_json, members are compared one after another, in document order, stopping at the first match. Each comparison first checks the key's length -- already known from the index, without reading the key bytes -- before comparing its content.

Notes

This method always returns #!cpp false when the value is not an object -- including a discarded view.

!!! info "Postconditions"

If `#!cpp v.contains(key)` returns `#!cpp true`, then `#!cpp v[key]` is not [discarded](is_discarded.md).

Examples

??? example

The example below counts how many of a batch of records carry an optional `retry_of` field, using `contains()`
to check without ever materializing a single record of the batch.

```cpp
--8<-- "examples/basic_json_view__contains.cpp"
```

Output:

```json
--8<-- "examples/basic_json_view__contains.output"
```

See also

Version history

  • Added in version 3.13.0.