-
-
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
Retrieve all values that match a json path #743
Comments
What about #include "json.hpp"
#include <iostream>
#include <regex>
using json = nlohmann::json;
int main(int argc, char const *argv[])
{
json j = R"({
"array": [
{
"a": 1,
"b": [10,11,12]
},
{
"a": 2,
"b": [20,21,22]
},
{
"a": 3,
"b": [30,31,32]
}
],
"cost": 1
})"_json;
auto f = j.flatten();
std::smatch sm;
std::regex re("/array/[^/]+/a");
json result;
for (auto it = f.begin(); it != f.end(); ++it)
{
std::regex_search(it.key(), sm, re);
if(not sm.empty())
{
result.push_back(it.value());
}
}
std::cout << result << std::endl;
} |
Hello, thank you for the follow up. I have already used this approach as a workaround. I was hoping that the library could take /array/*/a and reply back with a "vector" of values. Thanks. |
No, but you must admit that this is a very specific usecase... And I think you could clean up the code above and use some |
True, it is a specific issue. What kind of magic do you have in mind :-) ? |
I don't know, maybe something like |
Assuming that my json file is the following, I would like to collect all values that match with a regular expression using json_pointer. How can I get all values (integers) in the path /array/a/*?
I have used flatten() so far, that gives the "keys". But I don't know if I can query the json object to return an array of values that "match" a json path.
The text was updated successfully, but these errors were encountered: