-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to update a value for a nested(multi-level) json file #1344
Comments
I am trying to update a key in a nested json(sample json below) e.g. "user_name" to value "abc". I am unable to update the value using the iterator. Is there any function which would enable me to update the value?? Does the push_back or something already implements it? {
"server": {
"hostname": "192.168.xxx.xxx",
"port": 443,
"password": "632a1aaa-407f",
"user_name": "xyz"
}
} code snippet- void myIterator(json& j, const string configParam) {
string res = "";
for (auto& it : j.items()){
if (it.value().is_structured()) {
for (auto& it_meta : it.value().items()) {
std::cout << "\t" << it_meta.key() << " " << it_meta.value() << std::endl;
if (configParam.compare(it_meta.key()) == 0) {
it_meta.value().**push_back**(configParam,"abc");
break;
}
}
}
else {
std::cout << "\t" << it.key() << " " << it.value() << std::endl;
}
}
} |
So you want to achieve something like |
Yes , but the end user will only give the key and the parent tree “server” will not not be passed. |
Is that already being done in other ways ? I dont want to necessarily use iterators . Something that traverses the nested objects searches and adds/updates the key:values. |
You can assign to if (it.key() == "username")
el.value() = "abc"; should work. Furthermore, https://stackoverflow.com/a/46630394/266378 could be helpful. |
Thanks Neils. Will try this out. |
Thanks for the prompt response! This works to update the value if the Key is found. |
Just like in a |
Just like in a |
What is the issue you have?
Please describe the steps to reproduce the issue. Can you provide a small but working code example?
What is the expected behavior?
And what is the actual behavior instead?
Which compiler and operating system are you using? Is it a supported compiler?
Did you use a released version of the library or the version from the
develop
branch?If you experience a compilation error: can you compile and run the unit tests?
The text was updated successfully, but these errors were encountered: