1# <small>nlohmann::basic_json::</small>push_back 2 3```cpp 4// (1) 5void push_back(basic_json&& val); 6void push_back(const basic_json& val); 7 8// (2) 9void push_back(const typename object_t::value_type& val); 10 11// (3) 12void push_back(initializer_list_t init); 13``` 14 151. Appends the given element `val` to the end of the JSON array. If the function is called on a JSON null value, an 16 empty array is created before appending `val`. 17 182. Inserts the given element `val` to the JSON object. If the function is called on a JSON null value, an empty object 19 is created before inserting `val`. 20 213. This function allows using `push_back` with an initializer list. In case 22 23 1. the current value is an object, 24 2. the initializer list `init` contains only two elements, and 25 3. the first element of `init` is a string, 26 27 `init` is converted into an object element and added using `push_back(const typename object_t::value_type&)`. 28 Otherwise, `init` is converted to a JSON value and added using `push_back(basic_json&&)`. 29 30## Parameters 31 32`val` (in) 33: the value to add to the JSON array/object 34 35`init` (in) 36: an initializer list 37 38## Exceptions 39 40All functions can throw the following exception: 41 - Throws [`type_error.308`](../../home/exceptions.md#jsonexceptiontype_error308) when called on a type other than 42 JSON array or null; example: `"cannot use push_back() with number"` 43 44## Complexity 45 461. Amortized constant. 472. Logarithmic in the size of the container, O(log(`size()`)). 483. Linear in the size of the initializer list `init`. 49 50## Notes 51 52(3) This function is required to resolve an ambiguous overload error, because pairs like `{"key", "value"}` can be both 53 interpreted as `object_t::value_type` or `std::initializer_list<basic_json>`, see 54 [#235](https://github.com/nlohmann/json/issues/235) for more information. 55 56## Examples 57 58??? example "Example: (1) add element to array" 59 60 The example shows how `push_back()` and `+=` can be used to add elements to a JSON array. Note how the `null` value 61 was silently converted to a JSON array. 62 63 ```cpp 64 --8<-- "examples/push_back.cpp" 65 ``` 66 67 Output: 68 69 ```json 70 --8<-- "examples/push_back.output" 71 ``` 72 73??? example "Example: (2) add element to object" 74 75 The example shows how `push_back()` and `+=` can be used to add elements to a JSON object. Note how the `null` value 76 was silently converted to a JSON object. 77 78 ```cpp 79 --8<-- "examples/push_back__object_t__value.cpp" 80 ``` 81 82 Output: 83 84 ```json 85 --8<-- "examples/push_back__object_t__value.output" 86 ``` 87 88??? example "Example: (3) add to object from initializer list" 89 90 The example shows how initializer lists are treated as objects when possible. 91 92 ```cpp 93 --8<-- "examples/push_back__initializer_list.cpp" 94 ``` 95 96 Output: 97 98 ```json 99 --8<-- "examples/push_back__initializer_list.output" 100 ``` 101 102## Version history 103 1041. Since version 1.0.0. 1052. Since version 1.0.0. 1062. Since version 2.0.0. 107