Custom Object (JSON)

Defining a field as a Custom object allows you to enter field values as raw JSON. As long as you adhere to valid JSON syntax, you are free to define anything you like in your very own structure. If you have a JSON field with this content:

{
  "make": "Audi",
  "model": "RS6" 
}

you can use it directly in Liquid:

{% assign car = product.metafields.accentuate.specs %}

<p>My car is the brand new {{ car.make }} {{ car.model }}</p>

If you need the custom field as a client-side Javascript variable, you can use the JSON filter:

<script>
  let car = {{ product.metafields.accentuate.specs | json }}
  alert('My car is the brand new ' + car.make + ' ' + car.model);
</script>

Restrictions

JSON objects can be as simple or as complex as you need but if you need to create an array of JSON objects, either use repeatable fields (see separate article) or define the array as a property within the JSON object. An outer-most array is reserved for ACF's handling of repeatable JSON fields. So while this works well:

{
  "colors": ["Red", "Blue", "Green"]
}

this will cause ACF to split the values after you save them

["Red", "Blue", "Green"]

Last updated