Files
json/docs/mkdocs/docs/api/basic_json_view/items.md
T
Niels Lohmann c7df23666f Make view lookups last-wins and chained access safe
* Lookups (operator[], at, find, contains, count, value, JSON pointers)
  return the last member of a duplicate key, as materialize() and
  parse() keep it.
* operator[] on a discarded view returns a discarded view instead of
  throwing, so v["a"]["b"] is safe for a missing "a".
* operator[] and at() take any integer type (not only int and size_t),
  fixing ambiguous calls with unsigned, long, std::int64_t, ...
* Fix the operator[] documentation, which claimed a discarded view for
  a type mismatch where type_error.305 is thrown.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-09 16:11:33 +02:00

3.0 KiB

nlohmann::basic_json_view::items

/* unspecified */ items() const noexcept;

Returns a range of item values -- (key, value) pairs -- for use in range-based for loops. The key of an array element is its index, converted to a string, as for BasicJsonType::items().

The returned type is not part of the public API and may change between versions; use a range-based for loop (see the example), or #!cpp decltype(v.items()) if you need to name it.

for (const auto& item : v.items())
{
    std::cout << "key: " << item.key() << ", value: " << item.value() << '\n';
}

On C++17, item also supports structured bindings:

for (const auto [key, value] : v.items())
{
    std::cout << "key: " << key << ", value: " << value << '\n';
}

Note the #!cpp const auto (by value), not #!cpp const auto&: unlike BasicJsonType::items(), whose elements are references into an existing object, a view's item is produced on the fly for each step of the iteration, so there is nothing for a reference to bind to.

Return value

A range whose iterators dereference to item and whose #!cpp begin()/#!cpp end() are equivalent to basic_json_view::begin()/end(), in document order.

Exception safety

No-throw guarantee: this function never throws exceptions.

Complexity

Constant.

Notes

As for begin()/end(), items() visits every member of an object, including all occurrences of a duplicate key -- unlike operator[], at, find, contains, and count, which resolve to the last member with a given key. See the Notes on duplicate keys of operator[].

!!! danger "Lifetime issues"

As for `BasicJsonType::items()`, calling `items()` on a temporary view (or a temporary document) is dangerous:
the range refers back to the document, so the document must outlive the loop. See
[#2040](https://github.com/nlohmann/json/issues/2040) for the `BasicJsonType` background.

Examples

??? example

The example below shows a settings object whose source text records every update to a key as a duplicate
member, in the order they happened. `items()` walks all of them, so the update history is visible, while
[`operator[]`](operator[].md) sees the *last* one, and so does [`materialize()`](materialize.md) -- like
[`BasicJsonType::parse()`](../basic_json/parse.md).

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

Output:

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

See also

Version history

  • Added in version 3.13.0.