1# Iterators 2 3## Overview 4 5A `basic_json` value is a container and allows access via iterators. Depending on the value type, `basic_json` stores zero or more values. 6 7As for other containers, `begin()` returns an iterator to the first value and `end()` returns an iterator to the value following the last value. The latter iterator is a placeholder and cannot be dereferenced. In case of null values, empty arrays, or empty objects, `begin()` will return `end()`. 8 9 10 11### Iteration order for objects 12 13When iterating over objects, values are ordered with respect to the `object_comparator_t` type which defaults to `std::less`. See the [types documentation](types.md#key-order) for more information. 14 15??? example 16 17 ```cpp 18 // create JSON object {"one": 1, "two": 2, "three": 3} 19 json j; 20 j["one"] = 1; 21 j["two"] = 2; 22 j["three"] = 3; 23 24 for (auto it = j.begin(); it != j.end(); ++it) 25 { 26 std::cout << *it << std::endl; 27 } 28 ``` 29 30 Output: 31 32 ```json 33 1 34 3 35 2 36 ``` 37 38 The reason for the order is the lexicographic ordering of the object keys "one", "three", "two". 39 40### Access object key during iteration 41 42The JSON iterators have two member functions, `key()` and `value()` to access the object key and stored value, respectively. When calling `key()` on a non-object iterator, an [invalid_iterator.207](../home/exceptions.md#jsonexceptioninvalid_iterator207) exception is thrown. 43 44??? example 45 46 ```cpp 47 // create JSON object {"one": 1, "two": 2, "three": 3} 48 json j; 49 j["one"] = 1; 50 j["two"] = 2; 51 j["three"] = 3; 52 53 for (auto it = j.begin(); it != j.end(); ++it) 54 { 55 std::cout << it.key() << " : " << it.value() << std::endl; 56 } 57 ``` 58 59 Output: 60 61 ```json 62 one : 1 63 three : 3 64 two : 2 65 ``` 66 67### Range-based for loops 68 69C++11 allows to use range-based for loops to iterate over a container. 70 71```cpp 72for (auto it : j_object) 73{ 74 // "it" is of type json::reference and has no key() member 75 std::cout << "value: " << it << '\n'; 76} 77``` 78 79For this reason, the `items()` function allows to access `iterator::key()` and `iterator::value()` during range-based for loops. In these loops, a reference to the JSON values is returned, so there is no access to the underlying iterator. 80 81```cpp 82for (auto& el : j_object.items()) 83{ 84 std::cout << "key: " << el.key() << ", value:" << el.value() << '\n'; 85} 86``` 87 88The items() function also allows to use structured bindings (C++17): 89 90```cpp 91for (auto& [key, val] : j_object.items()) 92{ 93 std::cout << "key: " << key << ", value:" << val << '\n'; 94} 95``` 96 97!!! note 98 99 When iterating over an array, `key()` will return the index of the element as string. For primitive types (e.g., numbers), `key()` returns an empty string. 100 101!!! warning 102 103 Using `items()` on temporary objects is dangerous. Make sure the object's lifetime exeeds the iteration. See <https://github.com/nlohmann/json/issues/2040> for more information. 104 105### Reverse iteration order 106 107`rbegin()` and `rend()` return iterators in the reverse sequence. 108 109 110 111??? example 112 113 ```cpp 114 json j = {1, 2, 3, 4}; 115 116 for (auto it = j.begin(); it != j.end(); ++it) 117 { 118 std::cout << *it << std::endl; 119 } 120 ``` 121 122 Output: 123 124 ```json 125 4 126 3 127 2 128 1 129 ``` 130 131### Iterating strings and binary values 132 133Note that "value" means a JSON value in this setting, not values stored in the underlying containers. That is, `*begin()` returns the complete string or binary array and is also safe the underlying string or binary array is empty. 134 135??? example 136 137 ```cpp 138 json j = "Hello, world"; 139 for (auto it = j.begin(); it != j.end(); ++it) 140 { 141 std::cout << *it << std::endl; 142 } 143 ``` 144 145 Output: 146 147 ```json 148 "Hello, world" 149 ``` 150 151## Iterator invalidation 152 153| Operations | invalidated iterators | 154| ---------- | --------------------- | 155| `clear` | all | 156