1 # <small>nlohmann::basic_json::</small>operator[]
5 reference operator[](size_type idx);
6 const_reference operator[](size_type idx) const;
9 reference operator[](const typename object_t::key_type& key);
10 const_reference operator[](const typename object_t::key_type& key) const;
12 reference operator[](T* key);
14 const_reference operator[](T* key) const;
17 reference operator[](const json_pointer& ptr);
18 const_reference operator[](const json_pointer& ptr) const;
21 1. Returns a reference to the array element at specified location `idx`.
22 2. Returns a reference to the object element at with specified key `key`.
23 3. Returns a reference to the element at with specified JSON pointer `ptr`.
25 ## Template parameters
28 : string literal convertible to `object_t::key_type`
33 : index of the element to access
36 : object key of the element to access
39 : JSON pointer to the desired element
43 1. reference to the element at index `idx`
44 2. reference to the element at key `key`
45 3. reference to the element pointed to by `ptr`
49 Strong exception safety: if an exception occurs, the original value stays intact.
53 1. The function can throw the following exceptions:
54 - Throws [`type_error.305`](../../home/exceptions.md#jsonexceptiontype_error305) if the JSON value is not an array
55 or null; in that case, using the `[]` operator with an index makes no sense.
56 2. The function can throw the following exceptions:
57 - Throws [`type_error.305`](../../home/exceptions.md#jsonexceptiontype_error305) if the JSON value is not an object
58 or null; in that case, using the `[]` operator with a key makes no sense.
59 3. The function can throw the following exceptions:
60 - Throws [`parse_error.106`](../../home/exceptions.md#jsonexceptionparse_error106) if an array index in the passed
61 JSON pointer `ptr` begins with '0'.
62 - Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) if an array index in the passed
63 JSON pointer `ptr` is not a number.
64 - Throws [`out_of_range.402`](../../home/exceptions.md#jsonexceptionout_of_range402) if the array index '-' is used
65 in the passed JSON pointer `ptr` for the const version.
66 - Throws [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) if the JSON pointer `ptr` can
71 1. Constant if `idx` is in the range of the array. Otherwise, linear in `idx - size()`.
72 2. Logarithmic in the size of the container.
79 1. If the element with key `idx` does not exist, the behavior is undefined.
80 2. If the element with key `key` does not exist, the behavior is undefined and is **guarded by an assertion**!
82 1. The non-const version may add values: If `idx` is beyond the range of the array (i.e., `idx >= size()`), then the
83 array is silently filled up with `#!json null` values to make `idx` a valid reference to the last stored element. In
84 case the value was `#!json null` before, it is converted to an array.
86 2. If `key` is not found in the object, then it is silently added to the object and filled with a `#!json null` value to
87 make `key` a valid reference. In case the value was `#!json null` before, it is converted to an object.
89 3. `null` values are created in arrays and objects if necessary.
93 - If the JSON pointer points to an object key that does not exist, it is created and filled with a `#!json null`
94 value before a reference to it is returned.
95 - If the JSON pointer points to an array index that does not exist, it is created and filled with a `#!json null`
96 value before a reference to it is returned. All indices between the current maximum and the given index are also
97 filled with `#!json null`.
98 - The special value `-` is treated as a synonym for the index past the end.
102 ??? example "Example (1): access specified array element"
104 The example below shows how array elements can be read and written using `[]` operator. Note the addition of
105 `#!json null` values.
108 --8<-- "examples/operatorarray__size_type.cpp"
114 --8<-- "examples/operatorarray__size_type.output"
117 ??? example "Example (1): access specified array element"
119 The example below shows how array elements can be read using the `[]` operator.
122 --8<-- "examples/operatorarray__size_type_const.cpp"
128 --8<-- "examples/operatorarray__size_type_const.output"
131 ??? example "Example (2): access specified object element"
133 The example below shows how object elements can be read and written using the `[]` operator.
136 --8<-- "examples/operatorarray__key_type.cpp"
142 --8<-- "examples/operatorarray__key_type.output"
145 ??? example "Example (2): access specified object element"
147 The example below shows how object elements can be read using the `[]` operator.
150 --8<-- "examples/operatorarray__key_type_const.cpp"
156 --8<-- "examples/operatorarray__key_type_const.output"
159 ??? example "Example (3): access specified element via JSON Pointer"
161 The example below shows how values can be read and written using JSON Pointers.
164 --8<-- "examples/operatorjson_pointer.cpp"
170 --8<-- "examples/operatorjson_pointer.output"
173 ??? example "Example (3): access specified element via JSON Pointer"
175 The example below shows how values can be read using JSON Pointers.
178 --8<-- "examples/operatorjson_pointer_const.cpp"
184 --8<-- "examples/operatorjson_pointer_const.output"
189 - see [`at`](at.md) for access by reference with range checking
190 - see [`value`](value.md) for access with default value
194 1. Added in version 1.0.0.
195 2. Added in version 1.0.0. Overloads for `T* key` added in version 1.1.0.
196 3. Added in version 2.0.0.