skip to Main Content

I am using eBay’s new REST Sell API to create an inventory item. I am having problems creating product aspects manually. I’ve tried creating a list of name value pairs but eBay is returning the following error:

Could not serialize field [product.aspects]

Below is the request payload sample from eBay:

{
    "availability": {
        "shipToLocationAvailability": {
            "quantity": 50
        }
    },
    "condition": "NEW",
    "product": {
        "title": "GoPro Hero4 Helmet Cam",
        "description": "New GoPro Hero4 Helmet Cam. Unopened box.",
        "aspects": {
            "Brand": [
                "GoPro"
            ],
            "Type": [
                "Helmet/Action"
            ],
            "Storage Type": [
                "Removable"
            ],
            "Recording Definition": [
                "High Definition"
            ],
            "Media Format": [
                "Flash Drive (SSD)"
            ],
            "Optical Zoom": [
                "10x"
            ]
        },
        "imageUrls": [
            "http://i.ebayimg.com/images/i/182196556219-0-1/s-l1000.jpg",
            "http://i.ebayimg.com/images/i/182196556219-0-1/s-l1001.jpg",
            "http://i.ebayimg.com/images/i/182196556219-0-1/s-l1002.jpg"
        ]
    }
}

As far as I know product aspects are not fixed and can be anything, because of this I cannot create a Class. I’m not sure how to handle this apart from manually creating the JSON and inserting it in the correct place in the request payload.

Is there a better way to do this? Maybe create a dynamic object on the fly (any examples would help)?

2

Answers


  1. Chosen as BEST ANSWER

    I ended up creating a list of an Aspect object, using an extension method to manually convert the list into JSON which I deserialize into an object and pass into the request payload to eBay.

    Public Class Aspect
        Property Name As String
        Property Values As String()
    End Class
    
    Public Class RequestPayload
        <JsonProperty("aspects")>
        Public Property Aspects As Object
    End Class
    
    Sub Click()
    
        Dim Payload As New RequestPayload
    
        Payload.Aspects = (New List(Of Aspect) From {
                                New Aspect With {.Name = "Brand", .Values = {"GoPro"}},
                                New Aspect With {.Name = "Type", .Values = {"Helmet/Action"}}
                            }
                        ).ToAspectsObject()
    
    End Sub
    
    <Extension()>
    Function ToAspectsObject(Source As List(Of Aspect)) As Object
    
        Dim Aspects As New List(Of String)
    
        For Each Aspect In Source
            Aspects.Add(String.Format("""{0}"": [{1}]", Aspect.Name, String.Join(", ", Aspect.Values.Select(Function(x) String.Format("""{0}""", x)))))
        Next
    
        Dim JsonObject = String.Format("{{{0}}}", String.Join(", ", Aspects))
    
        Return Json.DeserializeObject(JsonObject)
    
    End Function
    

  2. I ended up modifying the OpenAPI contract to make this work.
    The Aspect is a System.Collections.Generic.ICollection<string> which will never serialize into the correct JSON, whatever you put in there. I changed it to a dynamic type.

        [Newtonsoft.Json.JsonProperty("aspects", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        //public System.Collections.Generic.ICollection<string> Aspects { get; set; }
        // EDITED
        public dynamic Aspects { get; set; }
    

    Then to add to the Aspects, this is my code:

    inventoryItem.Product.Aspects = new System.Dynamic.ExpandoObject();
    ((IDictionary<string, object>)inventoryItem.Product.Aspects).Add("Manufacturer", new List<string> {"Inoxia Ltd"});
    ((IDictionary<string, object>)inventoryItem.Product.Aspects).Add(inventory.Var1_Title_en, new List<string> 
    { inventory.Var1_Value_en });
    if (inventory.Var2_Title_en != "")
    {
        ((IDictionary<string, object>)inventoryItem.Product.Aspects).Add(inventory.Var2_Title_en, new List<string>{ inventory.Var2_Value_en });
    }
    

    This could possibly be done a bit neater, but this is working and uploads to eBay correctly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search