Skip to content
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

Filter with ID #1721

Closed
CN20222 opened this issue Mar 3, 2022 · 1 comment
Closed

Filter with ID #1721

CN20222 opened this issue Mar 3, 2022 · 1 comment
Labels

Comments

@CN20222
Copy link

CN20222 commented Mar 3, 2022

Hi how can i filter with Assistant this

[ { "id": 1, "ingredients": [ { "id": 5, "amount": 108 }, { "id": 8, "amount": 12 } ] }, { "id": 2, "ingredients": [ { "id": 4, "amount": 12 }, { "id": 6, "amount": 108 } ] }, { "id": 3, "ingredients": [ { "id": 4, "amount": 24 }, { "id": 6, "amount": 96 } ] }, { "id": 4, "ingredients": [ { "id": 4, "amount": 12 }, { "id": 5, "amount": 108 } ] }, { "id": 5, "ingredients": [ { "id": 4, "amount": 24 }, { "id": 5, "amount": 96 } ] } ]

for example only ID 2

[ { "id": 2, "ingredients": [ { "id": 4, "amount": 12 }, { "id": 6, "amount": 108 } ] } ]
Thanks for your help

@CN20222 CN20222 closed this as completed Mar 3, 2022
@CN20222 CN20222 reopened this Mar 3, 2022
@bblanchon
Copy link
Owner

Hi @CN20222,

Unfortunately, the filtering feature introduced in ArduinoJson 6.15 only allows including/excluding members based on the key, not the value. Admittedly, this is pretty limited but it's sufficient for many use cases and has a minimal impact on code size.

Filtering by value has been requested several times (#1486 (comment), #1316, #1708) but has not been implemented because it's much more complicated than it seems.
Indeed, there are tricky use cases to cover; for example, you could exclude all "users" objects whose age is "< 18" (and that's an easy one).
If you go down this road, you soon realize that you need the equivalent of a WHERE clause in SQL.

This could be implemented with a callback in the filter document, but this is incompatible with the design constraints for ArduinoJson 6, which forbids virtual functions.
Another solution would be to support JSONPath, but the language is complex, so the implementation would require a lot of work and would be quite big.

With the current implementation, your options are:

  1. to deserialize the entire document and loop through each member to find the right one (Filter by value #1708 (comment))
  2. to deserialize each object one by one and ignore the ones that don't match (see Deserialization in chunks).

Hopefully, one of these workarounds suits you.

Best regards,
Benoit

@CN20222 CN20222 closed this as completed Mar 4, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants