简体   繁体   English

使用液体模板语言连接对象数组

[英]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.

相关问题 在液体模板中迭代数组对象 - Iterate array object in liquid template 在 JSON object 的液体模板中正确格式化数组 - Properly formatting an array in a liquid template for a JSON object 我如何验证我的 JSON object 是否有另一个 object 或数组,使用液体模板 - How would I validate if my JSON object has another object or and Array, using Liquid Template 使用动态标签的液体模板转换 json - Transform json using liquid template for dynamic tags 使用 for 循环在 Liquid 模板中将 JSON 解析为 JSON。 如何使用 Liquid Template 遍历 JSON 中的列表? - Parsing JSON to JSON in Liquid Template using for loop. How to iterate through lists in JSON using Liquid Template? 在页面模板液体(Shopify)中显示来自settings_data.json的JSON数据数组 - Display JSON data array from settings_data.json in page template liquid (Shopify) 在 Azure 逻辑应用程序中,使用液体模板转换 xml 的优势是什么? - In Azure Logic apps, what is/are the advantage(s) of using a liquid template to convert xml? MailJet:使用模板 [ID] 发送消息时出现模板语言 - MailJet: A template language occurred when sending a message using Template [ID] 使用 Jinja2 模板将 Ansible 变量分配给 JSON 对象的动态数组 - Assign Ansible variable to dynamic array of JSON objects using Jinja2 template 在单个对象下加入多个对象并使用 jq -f .jq 添加兄弟对象 - Joining multiple objects under a single object and adding sibling objects using jq -f .jq
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM