Files
json/docs/mkdocs/docs/api/basic_json_view/begin.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

2.0 KiB

nlohmann::basic_json_view::begin

iterator begin() const noexcept;

Returns an iterator to the first element of an array, or the first member value of an object, in document order -- the order the values appear in the source text, not sorted by key. A primitive value iterates as a range of one element (itself); #!json null and a discarded view iterate as an empty range.

Return value

Iterator to the first element.

Exception safety

No-throw guarantee: this function never throws exceptions.

Complexity

Constant.

Notes

For an object, iteration visits every member, including all occurrences of a duplicate key -- unlike operator[], at, find, contains, and count, which all resolve to the first member with a given key. See the Notes on duplicate keys of operator[].

Because objects are iterated in document order rather than sorted by key, the order seen here can differ from what iterating the materialize()d BasicJsonType value would produce: a #!cpp basic_json object (std::map-backed by default) sorts its keys, while a view does not.

Examples

??? example

The example below iterates a log record's members with `begin()`/[`end()`](end.md) and prints them in the order
they were written. Materializing the record into a `BasicJsonType` object and iterating that would instead print
the members sorted by key.

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

Output:

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

See also

  • end - returns an iterator to one past the last element
  • cbegin - returns a const iterator to the first element
  • items - access iterator member functions in range-based for
  • BasicJsonType::begin - the corresponding function of basic_json

Version history

  • Added in version 3.13.0.