# How to exclude comma in last item in for loop that is prefaced by if condition (and output valid JSON)

**URL:** <https://talk.jekyllrb.com/t/how-to-exclude-comma-in-last-item-in-for-loop-that-is-prefaced-by-if-condition-and-output-valid-json/380>\
**Category:** Help\
**Created:** [April 1, 2017, 12:58am UTC](https://talk.jekyllrb.com/t/how-to-exclude-comma-in-last-item-in-for-loop-that-is-prefaced-by-if-condition-and-output-valid-json/380 "2017-04-01T00:58:23Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![tomjohnson1492](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/tomjohnson1492/32/39_2.png) [@tomjohnson1492](https://talk.jekyllrb.com/u/tomjohnson1492)\
**Post date:** [April 1, 2017, 3:55pm UTC](https://talk.jekyllrb.com/t/how-to-exclude-comma-in-last-item-in-for-loop-that-is-prefaced-by-if-condition-and-output-valid-json/380/4 "2017-04-01T15:55:29Z")

</div>

@rdyar That approach would normally work, but my actual scenario is a little more complex and I couldn’t quite wrangle the logic to work there. But thanks for responding.

@mmistakes Wow, this code actually works! Thank you. I realize I didn’t include a lot of details here, but you nailed it with what I needed.

In case anyone is interested, here’s the full code of the feed I was working on. The `recommendations` element has the code that mmistakes suggested:

```json
---
layout: null
---

[
  {% for ep in site.posts %}
  {
    "id": "{{ep.guid}}",
    "title": "{{ep.title}}",
    "description": "{{ep.summary}}",
    "duration": "{{ep.podcast_duration}}",
    "thumbURL": "{{site.url}}/{{ep.mp4_thumbnail | escape}}",
    "imgURL": "{{site.url}}/{{ep.mp4_thumbnail | escape}}",
    "videoURL": "{{ep.mp4_url}}",
    "categories": [
      "{{ep.meetup}}"
    ],
    "channel_id": "{{ep.categories}}",
    "recommendations":
     {% assign my_array = "" | split: ", " %}

     {% for tag in ep.tags %}
     {% for mypost in site.tags[tag] %}
     {% if mypost.url != ep.url %}
    {% assign my_array = my_array | push: {{mypost.guid}} %}
    {% endif %}
  {% endfor %}
{% endfor %}

{{my_array | jsonify}}

  } {% unless forloop.last %},{% endunless %}
{% endfor %},
{
"globalRecommendations": [
  {% assign globalRecs = site.posts | where: "global_recommendation", true %}
{% for ep in globalRecs %}
  "{{ ep.guid }}"{% unless forloop.last %},{% endunless %}
  {% endfor %}
]
}
]

```

This feed is used in a Fire TV app. I am generating recommended videos based on tags. Each post has a tag. The recommendations should include other videos that also have this tag, but not the video itself (which has the tag). You can see the result here: [http://podcast.writethedocs.org/fab.json](http://podcast.writethedocs.org/fab.json).

For example, in that feed link, the first title, WTD Episode 4, has the tag “docs-as-code”. The `recommendations` property includes other titles (the guid’s of those titles) that also have this same docs-as-code tag:

```auto
"recommendations": [
"wtdpodcast_ep3_docsascode",
"modern_tech_writing"
]

```

But as desired, WTD Episode 4 is not included in those recommendations.

(I imagine a similar technique could be used for related posts on a blog, though I’m guessing the build time would be considerable with a large number of items.)

Thanks again.

---

_[View the full topic](https://talk.jekyllrb.com/t/how-to-exclude-comma-in-last-item-in-for-loop-that-is-prefaced-by-if-condition-and-output-valid-json/380)._
