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

JSONL input #109

Open
nickolay opened this issue Oct 18, 2020 · 3 comments
Open

JSONL input #109

nickolay opened this issue Oct 18, 2020 · 3 comments

Comments

@nickolay
Copy link

mailmerge currently reads CSV files as data source.

For more complex templates, for instance containing one or more tables, a more appropriate format seems to be JSON, or, more specifically, json lines, which can be generated by some databases natively (see e.g. FOR JSON AUTO in MS SQL)

I have preliminary implementation in my branch: nickolay@02efaf2

@awdeorio
Copy link
Owner

I hadn't thought of this before. Maybe you could post a motivating example here, with a small template and a very small database? I'm having trouble understanding the problem with tables you mention above.

@nickolay
Copy link
Author

Oops, sorry! I mean a template like this:

FROM: [email protected]
TO: {{to_address}}
Subject: "Foo" changes to review
Content-Type: text/html

<html>
<head><meta http-equiv=content-type content="text/html; charset=utf-8"></head>
 <body>
Hello {{name}}!

The following changes to Foo might interest you:

<table><tr>
    <th>ID</th>
    <th>Name (old)</th>
    <th>Name (new)</th>
</tr>
{% for row in values %}
<tr>
    <td>{{row["id"]}}</td>
    <td>{{row["name_old"]}}</td>
    <td {{' style="background-color:#FFD966"' if row["name_old"] != row["name_new"] else '#' }}>{{row["name_new"]}}</td>
</tr>
{% endfor %}
</table>

...with a data source as follows we'll have two e-mails generated, each containing a two-row table:

{"to_address": "[email protected]", "name": "Nickolay1", "values": [{"id":5, "name_old": "Foo", "name_new": "Bar"}, {"id": 7, "name_old": "Baz", "name_new": "Baz" }]}
{"to_address": "[email protected]", "name": "Nickolay2", "values": [{"id":9, "name_old": "Qux", "name_new": "Bar"}, {"id": 7, "name_old": "Baz", "name_new": "Baz" }]}

With a CSV datasource the input to the template can only be a fixed number of text parameters (one for each CSV column). So I have to either provide the table's prerendered HTML in the CSV or use a more complex format, hence the JSON(L) idea.

@awdeorio
Copy link
Owner

awdeorio commented Nov 2, 2020

I can see this being helpful in a case where different recipients will have different numbers of values filled in. A table, like you said, is a good motivating example.

Testing would be important. Here are some things off the top of my head.

  • What if a line is empty?
  • What if a line contains malformed JSON?
  • What if a line contains a backslash?
  • What if a value contains a " character? What if it is escaped with a backslash?
  • What if a key contains a " character? What if it is escaped with a backslash?
  • Diverse JSON types: string, number, true, false, null
  • JSON list
  • JSON nested object

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

No branches or pull requests

2 participants