• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# basic_json::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 to use `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
401. The function can throw the following exceptions:
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"`
432. The function can throw the following exceptions:
44    - Throws [`type_error.308`](../../home/exceptions.md#jsonexceptiontype_error308) when called on a type other than
45      JSON object or null; example: `"cannot use push_back() with number"`
46
47## Complexity
48
491. Amortized constant.
502. Logarithmic in the size of the container, O(log(`size()`)).
513. Linear in the size of the initializer list `init`.
52
53## Notes
54
55(3) This function is required to resolve an ambiguous overload error, because pairs like `{"key", "value"}` can be both
56    interpreted as `object_t::value_type` or `std::initializer_list<basic_json>`, see
57    [#235](https://github.com/nlohmann/json/issues/235) for more information.
58
59## Examples
60
61??? example
62
63    The example shows how `push_back()` and `+=` can be used to add elements to a JSON array. Note how the `null` value
64    was silently converted to a JSON array.
65
66    ```cpp
67    --8<-- "examples/push_back.cpp"
68    ```
69
70    Output:
71
72    ```json
73    --8<-- "examples/push_back.output"
74    ```
75
76??? example
77
78    The example shows how `push_back()` and `+=` can be used to add elements to a JSON object. Note how the `null` value
79    was silently converted to a JSON object.
80
81    ```cpp
82    --8<-- "examples/push_back__object_t__value.cpp"
83    ```
84
85    Output:
86
87    ```json
88    --8<-- "examples/push_back__object_t__value.output"
89    ```
90
91??? example
92
93    The example shows how initializer lists are treated as objects when possible.
94
95    ```cpp
96    --8<-- "examples/push_back__initializer_list.cpp"
97    ```
98
99    Output:
100
101    ```json
102    --8<-- "examples/push_back__initializer_list.output"
103    ```
104
105## Version history
106
1071. Since version 1.0.0.
1082. Since version 1.0.0.
1092. Since version 2.0.0.
110