is there a way to be able to filter JSON payloads thru the Forest Admin GUI ?
In this example, i’d like to be able to filter by “payload - includes - “4. Communication””. But i can’t select the “payload” column in the filter, because i guess JSON i by default forbidden in your GUI
It is not supported by default as you mentioned. However I can suggest you to add a smart segment on your collection using the query mode to code the logic you want to filter on.
Thanks @anon16419211 for the tip,
This would be really interesting to provide (Json to string) to be able to search also on json content thru the list view.
Sadly you would have to migrate to the @forestadmin/agent to benefit from the Flattener which would answer your needs out of the box.
Supposing that you would stay on forest-express-sequelize, I could suggest that you create a Smart Field defined as a string, you could populate the field by extracting the desired properties from the JSON payload to then be able to filter/sort.
I would suggest sticking to the filter function available when defining a field, as it would result in the best performance. If not there is no need to define a Smart Field, you could just override the search on the whole collection.
You were not able to filter on your field because you were missing the isFilterable: true configuration on your field, as specified in the documentation of the Smart Field
In the same mindset of keeping the performance impact low, it would be best to restrict the path on which you search the value in your json, as the condition tree exposed is Sequelize’s. Here is what it would look like:
{
field: "payloadstring",
type: "String",
isFilterable: true,
get: (payload) => {
return JSON.stringify(payload);
},
filter({ condition, where }) {
switch (condition.operator) {
case "equal":
return {
"payload.file.id": condition.value,
};
case "contains":
return {
"payload.file.id": { [Op.like]: `%${condition.value}%` },
};
// ... And so on with the other operators not_equal, starts_with, etc.
default:
return null;
}
},
},
I’ve filled it with the path present in your last message. If you wish to make it work with multiple possible path I would suggest chaining them with an Or operator.
Thanks a lot @dogan, but it’d like to have results on the whole json string “payloadstring”, and not having to hardcode the searched values function of the payload object (payload.file.id for example).
Why ? Because my payloads have different structure, so i want the forest implementation to be generic whatever the payloads are.
I just want that:
if i perform a search with the string “d1533f47-afc9-4f98-aa74-f83db1bee9e8”, and it searches inside the key “payloadstring” and returns elements if the key “payloadstring” contains the string “d1533f47-afc9-4f98-aa74-f83db1bee9e8”
if i use the filter, using the condition “payloadstring” contains “d1533f47-afc9-4f98-aa74-f83db1bee9e8”, it should return the elements where “payloadstring” contains “d1533f47-afc9-4f98-aa74-f83db1bee9e8”
Otherwise using a custom field (here “payloadstring”) isn’t useful at all to filter json content…
However this might/will have an impact on performance depending on the size of your collection and the length of your payload column.
You can mitigate performance impact when using the global search by removing the search function (as it would be applied on every search you make on the collection). And only filter with contains when you wish to find the matching payload.