If I try to import the translation of a metafield specified as JSON, in all languages except the default one it is converted to a string, making the metafield unusable.
It also happens if I enter the same content, so it’s not a formatting problem
Hi @MattiaM , Jesse here from the Metafields team.
You are correct that translations for Metafields do come back as a string. As you have observed once it becomes a string you are not able to index into it as you would expect. I have created an issue on our side to dig into better alternatives for handling translated values. The only short term work around I can currently offer is to use the deprecated json_string type rather than json. The json_string type exists to support the JSON_STRING value type prior to the introduction of the updated metafield type system.
Have you got any news on this ? I’m stucked on the same problem : I need to create a json-type metafield with translations, but the translations seems being typed as string.
Therefor, I still don’t know how to iterate through every item of my JSON in order to render all the information in the translated languages. I have all technical information about my product in an extended format of the JSON below and all values have been translated correctly via API with a PIM integration.
We finally solved it by creating an extra metafield for the translated languages.
product.metafields.rocket.specifications for English
product.metafields.rocket.specifications_fr for French
product.metafields.rocket.specifications_de for German
And added the following snippet before pointing to the metafield, so it displays the correct JSON in each language instead of the translated JSON that was having issues (was only read as a string):
{%- if localization.language.iso_code == "en" -%}
{% assign specifications_json = product.metafields.rocket.specifications %}
{%- elsif localization.language.iso_code == "fr" -%}
{% assign specifications_json = product.metafields.rocket.specifications_fr %}
{% elsif localization.language.iso_code == "de" %}
{% assign specifications_json = product.metafields.rocket.specifications_de %}
{% else %}
{% assign specifications_json = product.metafields.rocket.specifications %}
{%- endif -%}
{%- if specifications_json and specifications_json.value -%}
{%- for specification in specifications_json.value -%}
{%- comment -%}
------------------------------------------------------------------------------
Here you would find the loop to display each item from the JSON metafield.
------------------------------------------------------------------------------
{%- endcomment -%}
{%- endfor -%}
{%- endif -%}
I understand Shopify is going to solve this JSON translation issue at some point, so we keep sending the translated JSON as both:
New language metafield (explained above)
Translation of the original English metafield.
@Jesse_Vogt if you have any news regarding this topic, please let us know, so we can correct the code accordingly and stay tuned about the best practices possible when translating JSON metafields.