craftcms / element-api

Create a JSON API/Feed for your elements in Craft.
MIT License
498 stars 56 forks source link

Remove data node to display a list of dictionaries #140

Closed williamhibberd closed 2 years ago

williamhibberd commented 3 years ago

I have been using the plugin following the steps here: https://klaviyoconnect.fostercommerce.com/Feed.html to create a Klaviyo product catalogue.

My data looks like this:

{
    "data": [
        {
            "id": "22981",
            "title": "Dan Dan Noodles",
            "description": "Bai Jia",
            "sku": "Dandannoodles",
            "url": "https://siteurl.com/products/dan-dan-noodles",
            "price": 3.2,
            "image": "http://storage.googleapis.com/image1"
        },
        {
            "id": "22961",
            "title": "Shanxi Dry Noodles",
            "description": "Bai Jia",
            "sku": "ShanxiNoodles",
            "url": "https://siteurl.com/products/shanxi-dry-noodles",
            "price": 3.2,
            "image": "http://storage.googleapis.com/image1"
        }
    ]
}

I am getting the error Invalid format. The JSON feed must be a list of dictionaries. when adding it to Klaviyo. Digging a bit deeper it looks like data is too deep, and I need to remove the "data" node as it's pushing the actual information that's needed too deep for Klaviyo to read.

Is there a way to remove the "data" node with the Element API plugin as per Klaviyos catalog example:

[
  {
    "id":"KLAVIYO-TSHIRT1",
    "title":"Classic Klaviyo T-Shirt 1",
    "link":"https://klaviyogear.myshopify.com/collections/klaviyo-classics/products/short-sleeve-t-shirt-1",
    "description":"Standard issue for all Klaviyos. This t-shirt has the Klaviyo logo on the front and mark diagram on the back.",
    "price":10,
    "image_link":"https://www.klaviyo.com/media/images/examples/products/klaviyo-tshirt-thumbnail.png",
    "categories":["apparel","t-shirt","new-arrival","swag"],
    "inventory_quantity":25,
    "inventory_policy":1
  },
  {
    "id":"KLAVIYO-TSHIRT2",
    "title":"Classic Klaviyo T-Shirt 2",
    "link":"https://klaviyogear.myshopify.com/collections/klaviyo-classics/products/short-sleeve-t-shirt-1",
    "description":"Standard issue for all Klaviyos. This t-shirt has the Klaviyo logo on the front and mark diagram on the back.",
    "price":10,
    "image_link":"https://www.klaviyo.com/media/images/examples/products/klaviyo-tshirt-thumbnail.png",
    "categories":["apparel","t-shirt","new-arrival","swag"],
    "inventory_quantity":30,
    "inventory_policy":1
  },
  {
    "id":"KLAVIYO-TSHIRT3",
    "title":"Classic Klaviyo T-Shirt 3",
    "link":"https://klaviyogear.myshopify.com/collections/klaviyo-classics/products/short-sleeve-t-shirt-1",
    "description":"Standard issue for all Klaviyos. This t-shirt has the Klaviyo logo on the front and mark diagram on the back.",
    "price":10,
    "image_link":"https://www.klaviyo.com/media/images/examples/products/klaviyo-tshirt-thumbnail.png",
    "categories":["apparel","t-shirt","new-arrival","swag"],
    "inventory_quantity":0,
    "inventory_policy":1
  }
]
brandonkelly commented 2 years ago

You would need to create a custom serializer class that formatted the response how you want. For example, see the built-in JsonFeedV1Serializer class, which formats the response according to the JSON Feed spec.