Inclusion filtering with Json Path #401
-
I have a list of JSON paths, I want to run them over a "random" json and keep only the objects/nodes that are matched by at least one Json Path. The way I've started doing was to expand the inclusionPaths into a list of all matched paths (like below). What I'm missing is a way to know if a certain jsonObject/jsonArray has one of those paths. json jsonObject;
std::vector<std::string> extractedPaths;
for (const std::string& path : inclusionPaths) {
auto result_options = jsonpath::result_options::path;
json new_paths_json = jsonpath::json_query(jsonObject, path, result_options);
for (auto val : new_paths_json.array_range()) {
extractedPaths.emplace_back(val.as<jsoncons::string_view>());
}
} Following this thought, is there a way to get the the path of an object? |
Beta Was this translation helpful? Give feedback.
Answered by
danielaparker
Nov 22, 2022
Replies: 1 comment 1 reply
-
If I understand you correctly, perhaps like this? json jsonObject = json::parse(R"({"foo":1,"bar":2 })");
auto inclusionPaths = std::unordered_set<std::string>{"$.foo", "$.baz"};
std::vector<std::pair<std::string,json>> extractedPathValuePairs;
auto callback = [&](const std::string& path, const json& val)
{
extractedPathValuePairs.emplace_back(path,val);
};
for (const std::string& path : inclusionPaths) {
jsonpath::json_query(jsonObject, path, callback, jsonpath::result_options::path);
}
for (const auto& item : extractedPathValuePairs)
{
std::cout << item.first << ", " << item.second << "\n";
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
augustoqm
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If I understand you correctly, perhaps like this?