Files
json/docs/mkdocs/docs/api/basic_json_view/items.md
T
Niels Lohmann 77acd4563c Add element access, iteration, values, and JSON pointers to json_view
Give basic_json_view the read-only access functions of basic_json:
operator[] and at() with keys and indices, front()/back(), find(),
contains(), count(), begin()/end() and cbegin()/cend(), items()
with structured bindings from C++17 on, and type_name().

Exceptions have the ids and messages of the const functions of
basic_json. Where basic_json has undefined behavior the view
answers safely: operator[] with a missing key or an out-of-range
index returns a discarded view, and front()/back() of an empty
container throw invalid_iterator.214. Objects are iterated in
document order, and all members are visited; duplicate-key lookups
find the first member (as yyjson and simdjson do), while parse(),
materialize(), and the map conversions keep the last value, as
parse() does. Keys of up to 16 bytes are compared with two
overlapping loads.

Add value conversions: get<T>()/get_to() for arithmetic types,
bool, nullptr_t, strings (std::basic_string copied,
string_view_t without a copy), BasicJsonType, views, std::vector,
and maps with string keys; get_string() for the string without a
copy; number_token() for the number exactly as written in the
source; value() with keys and JSON pointers; and operator[]/at()/
contains() with JSON pointers. Everything else, including types
with from_json(), goes through materialize() of that subtree.
get<T>() of arithmetic types is inlined down to the conversion, so
reading an integer needs no call.

detail::json_pointer_access exposes a pointer's reference tokens
to code outside basic_json.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
2026-10-07 16:42:26 +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 first 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) only ever sees the *first* one and [`materialize()`](materialize.md) -- like
[`BasicJsonType::parse()`](../basic_json/parse.md) -- keeps only the *last*.

```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.