0
resp, err := http.Get(url + t.Keywords[0] + ".json")
if err != nil {
    fmt.Println("error while getting productsRaw")
}
productsRaw, err := ioutil.ReadAll(resp.Body)
if err != nil {
    fmt.Println("error while parsing body")
}
productsRawString := string(productsRaw)
productsData := ShopifyProducts{}
json.Unmarshal([]byte(productsRawString), &productsData)
fmt.Println(productsData.Products)\

this is my code for encoding json file from website and it works if it encoding more than one product

{"product":{"id":4420737073222,"title":"W AIR MAX VERONA","body_html":"\u003cp\u003e\u003cspan\u003eDesigned with every woman in mind, the mixed-material upper features a plush collar, flashy colours and unique stitching patterns. Nike Air cushioning combines with the lifted foam heel for a modern touch, adding comfort and style to your journey.\u003c\/span\u003e\u003c\/p\u003e\n\u003cp\u003e \u003c\/p\u003e\n\u003cp\u003e\u003cspan\u003e-Smooth 

for example this what Println(productsRawString) show, so url works, but json.Unmarshal([]byte(productsRawString), &productsData) does't work.

type ShopifyProducts struct {
Products []struct {
    BodyHTML  string `json:"body_html"`
    CreatedAt string `json:"created_at"`
    Handle    string `json:"handle"`
    ID        int    `json:"id"`
    Images    []struct {
        CreatedAt  string        `json:"created_at"`
        Height     int           `json:"height"`
        ID         int           `json:"id"`
        Position   int           `json:"position"`
        ProductID  int           `json:"product_id"`
        Src        string        `json:"src"`
        UpdatedAt  string        `json:"updated_at"`
        VariantIds []interface{} `json:"variant_ids"`
        Width      int           `json:"width"`
    } `json:"images"`

ShopifyProducts structure.

Whats the problem with Unmarshal?

4
  • 1
    Unmarshal returns an error which you are not capturing. Do this & inspect the error to hint at the root cause. Commented Mar 14, 2020 at 13:51
  • <nil> this error I got Commented Mar 14, 2020 at 13:56
  • Does the API return a list of products? Your ShopifyProducts struct expects a list of Product but the json only have one object (instead of a list). Commented Mar 14, 2020 at 14:43
  • 2
    @LTUniger the json you've provided is incomplete, the struct type you've provided is incomplete. If you're looking for help then you need to first provide code that can actually be used to recreate the issue. Commented Mar 14, 2020 at 14:45

1 Answer 1

2

As I can see in your ShopifyProducts struct you have declared Products as an array. But in your serialized string product is an object. So it's not able to unmarshal that raw string.

You are also stating that it's working in case of product is more than one. Its working coz Product in your struct is an array.

Possible solutions:

  • Pre-process your raw string and bind your product in an array when it's not already an array. This way you will be able to Unmarshal it in a common way. But this will a workaround where you need to write unnecessarily write pre-processing code.
  • Just change the data from where you are getting it. Always save the product as an array, this will you can hit a GET call from your endpoint and it will always have a common format.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.