Array of objects in metafield on Shopify

I want each product to have a list of ingredients with categories associated with each product on Shopify. We haven’t entirely decided whether we want to structure this data hierarchically or not, but the two possible data shapes would look something like this if I inserted them directly into algolia:

{
  "name": "Test product 1",
  "ingredients": [
    {
      "name": "Milk",
      "category": "Dairy"
    },
    {
      "name": "Spinach",
      "category": "Vegetables"
    }
  ]
}
{
  "name": "Test product 2",
  "ingredients": [
    {
      "name": "Milk",
      "categories": {
        "lvl0": "Dairy",
        "lvl1": "Dairy > Cow Dairy"
      }
    },
    {
      "name": "Spinach",
      "categories": {
        "lvl0": "Vegetables",
        "lvl1": "Vegetables > Green Leafy Vegetables"
      }
    }
  ]
}

Originally I thought the ingredients field would be too complex to store in a metafield. I tried just pasting the object with the “string array” option which of course didn’t work and came out an escaped string. Any other suggestions?

Hi @patrick3,

Thanks for reaching us!

Have you tried to follow the required format for the “string array” type conversion as explained in our Metafields doc?

String Array: Convert a string to an array of strings, splitted on commas.

  • If you want to include a comma in a substring, wrap the substring in quotes:
    abc, “def,ghi”
  • If you want to include a quote in a substring, wrap the substring in quotes and escape the quote:
    abc, “def"ghi”
  • If you want to include a backslash in a substring, wrap the substring in quotes and escape the backslash:
    abc, “def\ghi”

Regards,

@charly.poly Yes, here is exactly what I put in the ingredients metafield:

"{
  \"name\": \"Milk\",
  \"category\": \"Dairy\"
}",
"{
  \"name\": \"Spinach\",
  \"category\": \"Vegetables\"
}"

It came out as a string in Algolia.
image

So I can’t filter on it. This isn’t surprising, it said string array and that’s exactly what I got. Just wondering if there is a way I can use an object array instead, or if there is some other method to using metafields or named tags so I can get this shape in algolia? Otherwise I am afraid I might have to upload this data from outside Shopify.

@charly.poly and/or anyone else, I still haven’t found a solution for this. Has anyone seen a similar use case?

These are the closest previous questions I could find:

For what it’s worth I got sick of trying to make Shopify work in a way that was flexible enough for this to work. We’re probably going to migrate to Magento2, way more flexible and I don’t have to deal with Shopify’s BS.