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)?
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