[英]Joining array of objects using liquid template language
I'm building a liquid template to help convert some XML to JSON.我正在构建一个液体模板来帮助将一些 XML 转换为 JSON。
Sample XML input:样品 XML 输入:
<ticket>
<account-id type="integer">123456</account-id>
<cc-email>
<cc-emails type="array">
<cc-email>abc@email.com</cc-email>
<cc-email>xyz@email.com</cc-email>
</cc-emails>
<fwd-emails type="array">
<fwd-email>abc@email.com</fwd-email>
<fwd-email>xyz@email.com</fwd-email>
</fwd-emails>
</cc-email>
</ticket>
Desired JSON output:所需的 JSON output:
{
"account-id":"123456",
"cc-email":"abc@email.com,xyz@email.com",
"fwd-email":"abc@email.com,xyz@email.com"
}
Liquid template attempt 1:液体模板尝试 1:
{
"account-id":"{{ ticket.account-id }}",
{% assign list = '' | split: ',' %}
{% for item in ticket.cc-email.cc-emails %}
{% assign list = list | push: item %}
{% endfor %}
"cc-email":"{{ list | join: ',' }}",
{% assign list = '' | split: ',' %}
{% for item in ticket.cc-email.fwd-emails %}
{% assign list = list | push: item %}
{% endfor %}
"fwd-email":"{{ list | join: ',' }}"
}
Liquid template attempt 2:液体模板尝试 2:
{
"account-id":"{{ ticket.account-id }}",
{% assign list = '' | split: ',' %}
{% for item in ticket.cc-email.cc-emails %}
{% assign list = list | push: item.cc-email %}
{% endfor %}
"cc-email":"{{ list | join: ',' }}",
{% assign list = '' | split: ',' %}
{% for item in ticket.cc-email.fwd-emails %}
{% assign list = list | push: item.fwd-email %}
{% endfor %}
"fwd-email":"{{ list | join: ',' }}"
}
I've also tried appending the items to a string.我也尝试将这些项目附加到一个字符串中。 No matter the method, I only get the following output:
不管用什么方法,我只得到如下output:
{
"account-id":"123456",
"cc-email":"",
"fwd-email":""
}
Can anyone help point out the issue?谁能帮忙指出问题? Seems like it has to be something simple but I haven't been able to find it.
似乎它必须是一些简单的东西,但我一直无法找到它。
Many thanks.非常感谢。
Ended up going back to appending strings.最终回到附加字符串。 Got this to work:
得到这个工作:
{% assign cc-email-list = '' %}
{% for item in ticket.cc-email.cc-emails %}
{% assign cc-email-list = cc-email-list | Append: item %}
{% if forloop.last == false %}
{% assign cc-email-list = cc-email-list | Append: ',' %}
{% endif %}
{% endfor %}
"cc-email":"{{ cc-email-list }}",
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.