-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
[Feature request] Access by path #1053
Comments
cc @martinRenou |
What is the difference to |
The difference is the unknown number of elements in the sequence. I guess that we could have an API based on iterator pairs for the sequence, and have the |
What about an accessor with an iterator pair
although the value type for the iterator should be a string / integral, or a variant containing string or some integral type. I guess it is not that simple. |
Isn't json_pointer what you're looking for? |
@theodelrieu is right. JSON Pointers can do this: #include <iostream>
#include "json.hpp"
using json = nlohmann::json;
int main()
{
json j = R"({
"foo": {
"bar": {
"value": 1
},
"baz": {
"value": 3
}
}
})"_json;
std::cout << j["/foo/baz"_json_pointer] << std::endl;
} |
Thanks! I did not know about this! |
Stupid question, but how do you use |
Please be sure to review the code of conduct and be respectful of other users. cc/ @nlohmann |
json_pointer is also a class. You can pass a string to create a JSON Pointer. |
Could you give an example? I'm trying to do something like this, but I don't understand why it won't compile:
|
What is the compiler error? |
This is the first one:
Also:
|
Seems that it just had to be |
This code prints |
@anirudh582 Works fine here: https://godbolt.org/z/ExaTeeMTd What version/compiler are you using? |
I am using version 3.10.5. I checked with the latest version and it is working. Thank you! |
|
I checked this and |
It would be great if there was accessors by path in a JSON object:
for example, if we have a JSON object
x
containingx[{"foo", "baz"}]
would return a JSON object containingThe text was updated successfully, but these errors were encountered: