Skip to content
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

Closed
soumyaranjanbej opened this issue Nov 7, 2018 · 9 comments
Closed
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@soumyaranjanbej
Copy link

  • 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?

@soumyaranjanbej
Copy link
Author

soumyaranjanbej commented Nov 7, 2018

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;
		}
	}
}

@nlohmann
Copy link
Owner

nlohmann commented Nov 8, 2018

So you want to achieve something like server["username] = "abc"; with iterators?

@soumyaranjanbej
Copy link
Author

Yes , but the end user will only give the key and the parent tree “server” will not not be passed.

@soumyaranjanbej
Copy link
Author

So you want to achieve something like server["username] = "abc"; with iterators?

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.

@nlohmann
Copy link
Owner

nlohmann commented Nov 9, 2018

You can assign to it.value(), so code like

if (it.key() == "username")
  el.value() = "abc";

should work.

Furthermore, https://stackoverflow.com/a/46630394/266378 could be helpful.

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Nov 9, 2018
@soumyaranjanbej
Copy link
Author

Thanks Neils. Will try this out.

@soumyaranjanbej
Copy link
Author

You can assign to it.value(), so code like

if (it.key() == "username")
  el.value() = "abc";

should work.

Furthermore, https://stackoverflow.com/a/46630394/266378 could be helpful.

Thanks for the prompt response! This works to update the value if the Key is found.
Please let me also know how to add a new key:value pair if the Key is not found.

@nlohmann
Copy link
Owner

Just like in a std::mal.

@nlohmann
Copy link
Owner

Just like in a std::map.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants