Adam Scot
Adam Scot

Reputation: 1409

Get Shopify JSON metafield as raw JSON data

If I create a metafield in Shopify and set its type to JSON, it is changed when outputted in liquid.

For example: [{"name":"Adam"}] outputs as [{"name"=>"Adam"}]

I assume that this is Shopify outputting the data as a kind of liquid object, which we can access using liquid.

In my case though, I need to be able to access the metafield data as raw JSON data, as it was inputted.

I have tried changing the metafield type to plain text. This actually works, but I still want to retain the ability of parsing the data with liquid.

I am also able to access this raw data using the Shopify Storefront API, but this seems overkill for what should be a relatively straightforward thing.

I have also tried using a function to replace "=>" with ":". Apart from feeling hacky, this doesn't actually product valid JSON when it has nested objects.

Is there a simple way to do this?

Upvotes: 0

Views: 1989

Answers (1)

David Lazar
David Lazar

Reputation: 11427

If you see the key:value emitted from Shopify as {"wingus" => "wongus"} perhaps you can pass that same key:value pair through the Liquid JSON filter and fix that?

{{ product.metafield.ffcc.qwerty.value | json }}

I am also very surprised the Liquid render provides the JSON value without doing that. I wrote a bug report about that some time ago to no effect. But anyway, passing through the filter works and you can immediately use the JSON to no ill-effect.

Upvotes: 2

Related Questions