On 06-06-17 10:32, Lukas via Boost wrote:
boost::property_tree::ptree loadedJSON; boost::property_tree::read_json("animalList.json", loadedJSON); auto assoc_it = loadedJSON.find("animals"); auto it = loadedJSON.to_iterator(assoc_it); it = it->second.begin(); it++; // move to second item loadedJSON.erase(it); boost::property_tree::write_json("shorterList.json", loadedJSON);
You're using erase on the wrong container. auto it = loadedJSON.to_iterator(assoc_it); Here, it points into the root ptree. it = it->second.begin(); Now, it points into the nested ptree ("animals") loadedJSON.erase(it); That's undefined behaviour. Fix: #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> #include <iostream> using boost::property_tree::ptree; int main() { ptree loadedJSON; read_json("animalList.json", loadedJSON); //auto& animals = *loadedJSON.find("animals"); auto& animals = loadedJSON.get_child("animals"); auto it = animals.begin(); it++; // move to second item animals.erase(it); write_json("shorterList.json", loadedJSON); }