xref: /third_party/json/docs/examples/update.cpp (revision c5f01b2f)
1#include <iostream>
2#include <iomanip>
3#include <nlohmann/json.hpp>
4
5using json = nlohmann::json;
6using namespace nlohmann::literals;
7
8int main()
9{
10    // create two JSON objects
11    json o1 = R"( {"color": "red", "price": 17.99, "names": {"de": "Flugzeug"}} )"_json;
12    json o2 = R"( {"color": "blue", "speed": 100, "names": {"en": "plane"}} )"_json;
13    json o3 = o1;
14
15    // add all keys from o2 to o1 (updating "color", replacing "names")
16    o1.update(o2);
17
18    // add all keys from o2 to o1 (updating "color", merging "names")
19    o3.update(o2, true);
20
21    // output updated object o1 and o3
22    std::cout << std::setw(2) << o1 << '\n';
23    std::cout << std::setw(2) << o3 << '\n';
24}
25