MCPcopy Create free account
hub / github.com/danielaparker/jsoncons / search_for_and_replace_a_value

Function search_for_and_replace_a_value

examples/src/jsonpath_examples.cpp:676–712  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

674}
675
676void search_for_and_replace_a_value()
677{
678 std::string data = R"(
679 { "books": [
680 { "author": "Nigel Rees",
681 "title": "Sayings of the Century",
682 "isbn": "0048080489",
683 "price": 8.95
684 },
685 { "author": "Evelyn Waugh",
686 "title": "Sword of Honour",
687 "isbn": "0141193557",
688 "price": 12.99
689 },
690 { "author": "Herman Melville",
691 "title": "Moby Dick",
692 "isbn": "0553213113",
693 "price": 8.99
694 }
695 ]
696 }
697 )";
698
699 auto j = jsoncons::json::parse(data);
700
701 // Change the price of "Moby Dick" from $8.99 to $10
702 jsonpath::json_replace(j,"$.books[?(@.isbn == '0553213113')].price",10.0);
703
704 // Increase the price of "Sayings of the Century" by $1
705 auto f = [](const std::string& /*location*/, jsoncons::json& value)
706 {
707 value = value.as<double>() + 1.0;
708 };
709 jsonpath::json_replace(j, "$.books[?(@.isbn == '0048080489')].price", f); // (since 0.161.0)
710
711 std::cout << pretty_print(j) << '\n';
712}
713
714void union_example()
715{

Callers 1

mainFunction · 0.85

Calls 3

parseFunction · 0.85
json_replaceFunction · 0.85
pretty_printFunction · 0.85

Tested by

no test coverage detected