-
I would like to filter my json object, returning a json array with a filtered content.
I would like to filter the JSON objects that have the
So, inside the
Is it possible? My system:
|
Beta Was this translation helpful? Give feedback.
Answered by
nlohmann
Jul 6, 2020
Replies: 2 comments 1 reply
-
You can use any STL algorithm you like, e.g. #include <iostream>
#include "json.hpp"
using json = nlohmann::json;
int main()
{
json j = R"({
"myArray":
[
{
"key": "A",
"name": "John"
},
{
"key": "B",
"name": "Steve"
},
{
"key": "A",
"name": "Roger"
}
]
})"_json;
json filtered;
std::copy_if(j["myArray"].begin(), j["myArray"].end(),
std::back_inserter(filtered), [](const json& item) {
return item.contains("key") && item["key"] == "A";
});
std::cout << filtered << std::endl;
} Output: [{"key":"A","name":"John"},{"key":"A","name":"Roger"}] |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
nyckmaia
-
For in-place filtering one can use the Erase-remove pattern |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use any STL algorithm you like, e.g.
std::copy_if
: