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

Add jinja fromjson custom filter to notification template handling #2378

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

delenamalan
Copy link

@delenamalan delenamalan commented May 19, 2024

Add a custom Jinja2 filter, fromjson, to convert a JSON string to JSON - which can then be used by Jinja.

This is useful for formatting notifications based on JSON API responses.

If the API route you're watching returns a JSON list, for example, you could format a notification like this:

<ul>
{% for item in current_snapshot|fromson %}
  <li>{{ item.title}}: {{ item.price }}</li>
{% endfor %}
</ul>

@dgtlmoon
Copy link
Owner

dgtlmoon commented May 20, 2024

Can you give some (any) information here? whats your use case exactly?

any examples??

@delenamalan
Copy link
Author

@dgtlmoon sure, I added a description and example to the PR description now.

The PR is still in draft because I'd still like to add a test and update the docs.

@nmbgeek
Copy link

nmbgeek commented Jul 7, 2024

This would be very helpful. I am currently having to use jinja macros to very crudely parse json returned by jq.

@dgtlmoon dgtlmoon changed the title Add jinja fromjson custom filter Add jinja fromjson custom filter to notification template handling Jul 9, 2024
@dgtlmoon
Copy link
Owner

dgtlmoon commented Jul 9, 2024

Few things

  • whats the security risk with this? anyone can make json.loads run.. https://software.codidact.com/posts/284628 maybe it has some bug and can be exploited?
  • It needs a test added
  • UI should be updated with a note to say that its possible
  • Needs exception handling (what happens if it doesnt actually get JSON data? what should it output? or just throw error in the UI?)
  • Needs handling for large strings to avoid DoS

Maybe do something automatic like, if the document begins with { and ends with } then add json_snapshot as a token in the notification settings.... or only actually process json_snapshot if its in the notification body

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants