0

I'm using Eleventy and NetlifyCMS and I want one field that is a string (sepparated by comma) to convert to an array in JSON so I can search individual items in that field from Fuse.js

So I wrote a .njk file that builds a JSON index with all the data I need to search in.

The front matter in .md files from Eleventy:

---
mylistfield: item 1, item 2, item 3
---

Then i'm taking the data to build a JSON file with a .njk file:

---
permalink: '../search.json'
eleventyExcludeFromCollections: true
---

[  
  {% for post in collections['plantas'] %} 
  { 
    "myarrayofitems":"{{ post.data['mylistfield'] }}",


  }
  {% if loop.last == false %},{% endif %} {% endfor %}  
]

Current search.json output:

[  
   
  { 
    "mylistfield":"item 1, item 2, item 3"
  }
  ,  
  { 
    "mylistfield":"item 1, item 2, item 3"
  }
] 

Expected search.json output:


[  
   
  { 
    "mylistfield":["item 1", "item 2", "item 3"]
  }
  ,  
  { 
    "mylistfield":["item 1", "item 2", "item 3"]
  }
] 

The problem is that I get a string separated by comma and I want to split to an array with each item. Is it posible to do that in that Nunjucks file?

Thanks

  • `.data['mylistfield'].split(',')` or `... | split(',')`? – Aikon Mogwai Dec 03 '21 at 19:21
  • Thanks @AikonMogwai but it's giving me an error: `Error: filter not found: split"` Do I need something special for that to work? – Diego de Sousa Dec 04 '21 at 11:50
  • Oh, sorry, Nunjucks doesn't have `split`-filter by default. So you need to implement it or use `js`-version. – Aikon Mogwai Dec 04 '21 at 15:31
  • I did a bit of research now with your info but I think is out of my scope of knowledge. Found "lodash.split" and even the ".split" is used in some parts of my setup (Eleventy Filters + NetlifyCMS) but I didn't code that. Tried to install the package but then I don't know where to call for it because I'm inside a .njk file. – Diego de Sousa Dec 04 '21 at 20:35
  • You don't need to install addition packages. You can use js-function inside `{{ }}` e.g. `{% for post in text.split(',') %}`. I read your question again. Try `dump`-filter over the array. – Aikon Mogwai Dec 04 '21 at 23:15
  • Thanks for your help but I'm not sure how to implement that. Did some tests but the error log is not helping me that much. So I have this: `"myarrayofitems":"{{ post.data['mylistfield'] }}",` but I'm not sure how to write that js function inside it. – Diego de Sousa Dec 06 '21 at 15:38
  • `{{ post.data['mylistfield'].split(',') | dump }}`? Honestly I don't fully understand what you have and what you need. – Aikon Mogwai Dec 06 '21 at 16:18
  • Ok, I changed the initial question to make my problem easier to understand. – Diego de Sousa Dec 06 '21 at 17:22
  • Try `"myarrayofitems":{{ post.data['mylistfield'].split(',') | dump }}`. – Aikon Mogwai Dec 06 '21 at 20:53
  • I get the following error `Error: Unable to call post["data"]["mylistfield"]["split"], which is undefined or falsey` – Diego de Sousa Dec 06 '21 at 23:28
  • 1. `post["data"]["mylistfield"] ` is equal to `post.data.mylistfield`. 2. It looks like some of them is null, so you need apply "not null"-operator: `"myarrayofitems": {{ (post.data.mylistfield or '').split(',') | dump }}` – Aikon Mogwai Dec 07 '21 at 00:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239896/discussion-between-diego-de-sousa-and-aikon-mogwai). – Diego de Sousa Dec 07 '21 at 00:51

0 Answers0