simonw / datasette

An open source multi-tool for exploring and publishing data
https://datasette.io
Apache License 2.0
9.59k stars 691 forks source link

from_json jinja2 filter #1003

Open mhalle opened 4 years ago

mhalle commented 4 years ago

When JSON fields are rendered in a jinja2 template, it is handy to be able to manipulate them as data (e.g., iterate over an array of values).

Ansible has a "from_json" function, which just called json.loads. It's a trivial as a datasette plugin, but it seems generally useful. Does it makes sense to add it directly into the app?

simonw commented 4 years ago

Yes I think that makes sense. I added json to the template context in Dogsheep Beta just a few days ago because I needed that: https://github.com/dogsheep/dogsheep-beta/blob/bed9df2b3ef68189e2e445427721a28f4e9b4887/dogsheep_beta/__init__.py#L176

simonw commented 4 years ago

I'm not a huge fan of from_json as the name for this. Some other options:

simonw commented 4 years ago

I think json_dumps() and json_loads() as aliases for json.dumps() and json.loads() is the way to go here.

mhalle commented 4 years ago

I agree on the descriptive and python-consistent naming. There is already a tojson, but frankly i find the "to" and "from" confusing in a text templating language where what's a string and what's data isn't 100% transparent.