Re: Value must be a stringified JSON object with a value (numeric) and unit

Solved

Value must be a stringified JSON object with a value (numeric) and unit

MGR-NZ
Tourist
9 1 3

Hi all

I have another Flow variable problem, most likely a syntax error but may be my precursor steps.

Objective:

I've set up a metafield 'custom.shipping_weight_grams' which I want to enter the product weight.  Sounds easy but there are two scenarios:

- product has no variants

- product has variants

But the variable path for both seems the same, to get to the weight value field you have to go down the variant path.  Regardless, I've set up this flow on product creation, then check if product.hasOnlyDefaultVariant then if yes, getproductvariantdata.  Then I choose update product metafield, select my field then navigate to 

product/variants/inventoryItem/measurement/weight/float
This defaults in this code for the value:
 
{% for variants_item in product.variants %}
{{variants_item.inventoryItem.measurement.weight.value}}
{% endfor %}
That gets an error "Value must be a stringified JSON object with a value (numeric) and unit"
I've tried many variations, I don't think the first line is relevant as I've already pulled the variant data.
The API documentation suggests the weight field is InventoryItemMeasurement.weight so I thought this might work: {{ InventoryItemMeasurement.weight | weight }} but that doesn't work.
Any help would be appreciated, have spent hours on this already!
Screenshots attached.
MGRNZ_0-1742830809126.jpeg

 

MGRNZ_0-1742831188440.png

 

M G Robinson
Accepted Solution (1)
paul_n
Shopify Staff
1815 199 431

This is an accepted solution.

Your metafield is a JSON type and you provided it with text. 

 

 

\n    200.0  \n

 

 

Make sure to remove any whitespace from your liquid around the JSON by using hyphens in your tags like {%- -%} or {{- -}}

 

The gray arrows mean that path was not followed.

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.

View solution in original post

Replies 8 (8)

RPiii
Shopify Staff
162 27 49

Hi there, I think you may be able to avoid that initial condition as well as the "Get product variant data" action by just using a "For each" to loop through the variants returned with the "Product created" trigger, like so: 

RPiii
Shopify Staff
162 27 49

Oh, and you may need to use the Update product variant metafield action instead.

MGR-NZ
Tourist
9 1 3

Thanks RPiii, I meant to use the productvariant update field but accidentally used the product one.  I created the decision fork a couple of steps back when checking the product.hasOnlyDefaultVariant value realising the end point needed to reflect that.  

I also took out the getproductvariantdata step but eventually put it back in.  The logic says although the create product trigger is singular, when you get data back for variants, it may be a single variant but the code must be written for it to be multiple variants even if the result is only one variant.

The error is still the same regardless.

MGRNZ_0-1742845843482.png

 

M G Robinson
MGR-NZ
Tourist
9 1 3

Hi RP

I revisited your idea and the setup looked a bit different.  The variable field for the Update Product Metafield step now looks like this:

{{variantsForeachitem.inventoryItem.measurement.weight.value}}

But it still doesn't work, same error.

The full text of the error reads: "Value must be a stringified JSON object with a value (numeric) and unit (string from one the supported measurement units) fields." For value: "\n 200.0\n", Type: weight

The key elements of this error are:

1. a stringified JSON object with a value (numeric) 

2. unit - a string from one of the supported measurement units

Is there another way to write this query specifically addressing those two elements?

 

This might be significant, why are the arrows grey in these two steps and a zero in the final arrow?

MGRNZ_1-1742989802580.jpeg

 

 

M G Robinson
paul_n
Shopify Staff
1815 199 431

This is an accepted solution.

Your metafield is a JSON type and you provided it with text. 

 

 

\n    200.0  \n

 

 

Make sure to remove any whitespace from your liquid around the JSON by using hyphens in your tags like {%- -%} or {{- -}}

 

The gray arrows mean that path was not followed.

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
MGR-NZ
Tourist
9 1 3

Hi Paul

Thanks for your reply.  I've tried your changes (I actually saw this from you in another discussion) but the same error persists.  This is my 'Update Product Metafield' setup:

MGRNZ_0-1743374021852.png

and the run analysis:

MGRNZ_1-1743374082485.png

 

M G Robinson
paul_n
Shopify Staff
1815 199 431

So first, I'm a bit confused by why you are writing a "product" metafield here for each variant...the last update is going to win. 

 

Regarding the error, you didn't read the part about JSON. You need to be entering JSON not a value. 

 

This doc has the various types and their formats:

https://shopify.dev/docs/apps/build/custom-data/metafields/list-of-data-types

 

Weight is:

{ 
 "value": 2.5, 
 "unit": "kg"
} 
Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
MGR-NZ
Tourist
9 1 3

Hi Paul

I didn't understand the significance of the solution when you first mentioned it, I thought the fix was in the liquid for the variable field.  I've since created metafields with json type and the flow is working as it should.

Thanks for your help, very much appreciated.

Cheers 

M G Robinson