Cannot sort a null object in a secondary collection in a data file

I have a data file that lists all my menu items. The file name is nav-yaml.yml and is stored in the _data folder and it looks like this:

items:
    - title: Home
      order: 2
    - title: Services
      order: 1
      subitems:
       - title: Consulting
         order: 3
       - title: Podcasting
         order: 2
       - title: Training
         order: 1

In the following code, I create a variable that reads the data and displays it based on the order value in the YML data file. That works for the top-level data elements (the items). However, when I attempt to sort the child items (the subitems), that fails and I get the following error:

Liquid Exception: Liquid error (line 39): Cannot sort a null object.

Here is the code. I included comments just so you can see where the problem is:

<!-- The following sort on the top-level items works -->
{% assign items = site.data.nav-yaml.items | sort: 'order' %}
{% for item in items %}
    {{ item.title }}
    <!-- The following sort on the child subitems fails: Liquid Exception: Liquid error (line 39): Cannot sort a null object. -->
    {% assign subitems = item.subitems | sort: 'order' %}
    {% for subitem in subitems %}
        {{ subitem.title }}
    {% endfor %}
{% endfor %}

I ran a trace and while I can supply the entire trace, here are the items that stuck out:

Liquid Exception: Liquid syntax error (line 17): Syntax Error in ‘for loop’ - Valid syntax: for [item] in [collection] in index.markdown
Error: Liquid syntax error (line 17): Syntax Error in ‘for loop’ - Valid syntax: for [item] in [collection]

/Library/Ruby/Gems/2.6.0/gems/jekyll-4.0.1/lib/jekyll/filters.rb:233:in `sort’: Liquid error (line 39): Cannot sort a null object. (Liquid::ArgumentError)

The Error in ‘for loop’ text leads me to believe maybe I am doing something wrong assigning the variable or properly forming the for loop. However, if I just change the code and remove the sort filter, the code works just fine so I do not think I am doing something invalid.

The cannot sort a null object error suggests I do not have an order for each subitem, but in fact, I do have that, so I do not believe that to be the problem.

Can you help me understand why the sort will not work on the secondary items in my data set?

1 Like

You are getting an error on the first item while the second is valid.

Add a value to the first

items:
    - title: Home
      order: 2
      subitems: []

A more scalable approach would be to get the value first and if it is not null then use it. So then you don’t every to set it everywhere.

So you’ll do something like

{% assign subitems = item.subitems %}
{% if subitems %}
  {% assign subitems = subitems | sort: 'order' %}
   ...

{% endif %}

Maybe you can use something like a default filter to replace null with [] so you don’t need an if block.

@MichaelCurrin Thank you very much! I had not even considered my data was wrong :slight_smile:

I also appreciate the idea of doing the if block.

It’s a pleasure. Glad to help