Solved

Liquid does not print object.field property

pdarceno
New Member
6 0 0

I'm new to Liquid and I'm trying to create a Mechanic script in Shopify to loop through all my orders and check each SKU. This is what I came up with:

 

 

 

 

 

{% for line_item in order.line_items %}
    {% if line_item.sku %}
        {{ line_item.sku }}
            {% capture sku %}

 

 

 

 

 

On the third line, I was expecting to see it print an order's SKU, which should look like this: 1xSAMPLE1+2xSAMPLE2. It, however, showed me this error:

 

 

 

 

 

lexical error: invalid char in json text.
                                      1xS
                     (right here) ------^

Typically, this comes down to a stray comma, or some other character that makes for invalid JSON.

Context (lines 2-5 of rendered JSON):

 

 

 

 

 

As I understand it, Mechanic (or Liquid) only wants to print JSON values. How do I view my variables while running the Mechanic script?

Here is the full code and the error log:

miWdA.png

 

UPDATE:

 

{% comment %}
  pseudocode:
  for line_item in order.line_items:
    if line_item.sku exists:
        products_and_quantities = split line_item.sku with '+'
        for product_and_quantity in products_and_quantities:
          new_order = split product_and_quantity with 'x'
            quantity = new_order[0]
            product = new_order[1]

            if product.sku exists in products:
              overwrite order: add quantity of product to order_list 

{% endcomment %}


{% for line_item in order.line_items %}
  {% if line_item.sku %}
    {% assign sku = line_item.sku %}
    {% capture products_and_quantities %}
      {{ sku | split: "+" }}
    {% endcapture %}
    {% for product_and_quantity in products_and_quantities %}
      {% capture new_order %}
        {{ product_and_quantity | split: "x" }}
      {% endcapture %}
      {% capture quantity %}
        {{ new_order | first }}
      {% endcapture %}
      {% capture product %}
        {{ new_order | last }}
      {% endcapture %}
    {% endfor %}  
  {% endif %}
{% endfor %}

 

Accepted Solution (1)
PaulNewton
Shopify Partner
6339 580 1350

This is an accepted solution.

@pdarceno Did fixing the capture tag syntax fix the error?

 

With liquid whenever possibly try to use the assign tag instead of capture to cut down on edge issues like whitespace, newlines , improve readability and as you've found avoid tag wrapping mistakes either from hand coding or autocompletion,etc. 

 

For mechanic specific issues also try the mechanic partner slack channel first since mechanic-liquid and it's other features is a smaller audience on the forums vs shopify-liquid.

https://learn.mechanic.dev/resources/slack.

 

Keep in mind that apps mechanics-liquid is not the same as shopify-liquid for the platform. They have overlap of some basics but are different flavors|implementations of the liquid template engine.

https://github.com/shopify/liquid  

https://learn.mechanic.dev/platform/liquid/ 

 

Also be aware some the differences are features for working with arrays in saner manner not needing the "| split:','" workaround required in shopify templates. And that you can make objects and hashes as well.

https://learn.mechanic.dev/platform/liquid/basics/types#array 

https://learn.mechanic.dev/platform/liquid/filters#array-filters 

https://learn.mechanic.dev/platform/liquid/basics/types#hash 

https://learn.mechanic.dev/platform/liquid/basics/types#object 

 

Goodluck.

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Buy Paul a Coffee for more answers or donate to eff.org


View solution in original post

Replies 5 (5)

Jason
Shopify Expert
11196 226 2287

You have an usual capture tag placement. Those are not wrapped around anything so won't be doing anything other than grabbing an empty string.

 

Did you intend to put them around the {{ new_order | first}} and last outputs?

 

Doesn't mean that is your prob but is what stands out for the section of code you've shown

★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★
pdarceno
New Member
6 0 0

ah. I just read the docs on capture. Will update the code, thanks for pointing that out. That was not what I intended. I thought how capture worked is it assigns the result of the previous line to the variable in the next line inside the capture tag

pdarceno
New Member
6 0 0

I believe this is syntactically correct:

{% comment %}
  pseudocode:
  for line_item in order.line_items:
    if line_item.sku exists:
        products_and_quantities = split line_item.sku with '+'
        for product_and_quantity in products_and_quantities:
          new_order = split product_and_quantity with 'x'
            quantity = new_order[0]
            product = new_order[1]

            if product.sku exists in products:
              overwrite order: add quantity of product to order_list 

{% endcomment %}


{% for line_item in order.line_items %}
  {% if line_item.sku %}
    {% assign sku = line_item.sku %}
    {% capture products_and_quantities %}
      {{ sku | split: "+" }}
    {% endcapture %}
    {% for product_and_quantity in products_and_quantities %}
      {% capture new_order %}
        {{ product_and_quantity | split: "x" }}
      {% endcapture %}
      {% capture quantity %}
        {{ new_order | first }}
      {% endcapture %}
      {% capture product %}
        {{ new_order | last }}
      {% endcapture %}
    {% endfor %}  
  {% endif %}
{% endfor %}

 

PaulNewton
Shopify Partner
6339 580 1350

This is an accepted solution.

@pdarceno Did fixing the capture tag syntax fix the error?

 

With liquid whenever possibly try to use the assign tag instead of capture to cut down on edge issues like whitespace, newlines , improve readability and as you've found avoid tag wrapping mistakes either from hand coding or autocompletion,etc. 

 

For mechanic specific issues also try the mechanic partner slack channel first since mechanic-liquid and it's other features is a smaller audience on the forums vs shopify-liquid.

https://learn.mechanic.dev/resources/slack.

 

Keep in mind that apps mechanics-liquid is not the same as shopify-liquid for the platform. They have overlap of some basics but are different flavors|implementations of the liquid template engine.

https://github.com/shopify/liquid  

https://learn.mechanic.dev/platform/liquid/ 

 

Also be aware some the differences are features for working with arrays in saner manner not needing the "| split:','" workaround required in shopify templates. And that you can make objects and hashes as well.

https://learn.mechanic.dev/platform/liquid/basics/types#array 

https://learn.mechanic.dev/platform/liquid/filters#array-filters 

https://learn.mechanic.dev/platform/liquid/basics/types#hash 

https://learn.mechanic.dev/platform/liquid/basics/types#object 

 

Goodluck.

Save time & money ,Ask Questions The Smart Way


Confused? Busy? Get the solution you need paull.newton+shopifyforum@gmail.com


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Buy Paul a Coffee for more answers or donate to eff.org


pdarceno
New Member
6 0 0

Thanks @PaulNewton. I used arrays for storing the values I got using assign instead of capture. I'll also check the slack community out.