Search code examples
jsongoresponseunmarshalling

Parse stringified JSON


I'm trying to parse stringified JSON with Go, but I cannot find anything that explains how do to efficient parsing.

Example JSON:

{
 "messages":"<ul class=\"messages\"><li class=\"success-msg\"><ul><li><span>Item succcessfully added<\/span><\/li><\/ul><\/li><\/ul>",
 "app_cart":"[]",
 "addcartrows":"[{\"productId\":\"1675688\",\"quantity\":1,\"unitPrice\":\"290.00\",\"currency\":\"EUR\",\"sku\":\"P00525485-3\"}]",
 "minicart_content":"<ul class=\"checkout-types minicart\">\n
<li>",
 "cart_qty":"1",
 "added_product_json":"{\"id\":\"1675695\",\"size\":\"40\"}"
}

I usually parse json by casting it to a struct. like this:

type AutoGenerated struct {
    Messages         string `json:"messages"`
    AppCart          string `json:"app_cart"`
    Addcartrows      string `json:"addcartrows"`
    MinicartContent  string `json:"minicart_content"`
    CartQty          string `json:"cart_qty"`
    AddedProductJSON string `json:"added_product_json"`
}
var j AutoGenerated 

if err = json.Unmarshal(body, &AutoGenerated); err != nil {
    fmt.Println(err) // json: cannot unmarshal string into Go struct field AutoGenerated.added_product_json
}

However I do not know how to correctly parse this type of response. Any help?


Solution

  • do it in two steps.

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    func main() {
        type AutoGenerated struct {
            Messages         string `json:"messages"`
            AppCart          string `json:"app_cart"`
            Addcartrows      string `json:"addcartrows"`
            MinicartContent  string `json:"minicart_content"`
            CartQty          string `json:"cart_qty"`
            AddedProductJSON string `json:"added_product_json"`
        }
        type addedProduct struct {
            ID   string `json:"id"`
            Size string `json:"size"`
        }
        type productRow struct {
            ProductID string `json:"productId"`
            Quantity  int    `json:"quantity"`
            UnitPrice string `json:"unitPrice"`
            Currency  string `json:"currency"`
            SKU       string `json:"sku"`
        }
        var j AutoGenerated
    
        body := []byte(`{
     "messages":"<ul class=\"messages\"><li class=\"success-msg\"><ul><li><span>Item succcessfully added<\/span><\/li><\/ul><\/li><\/ul>",
     "app_cart":"[]",
     "addcartrows":"[{\"productId\":\"1675688\",\"quantity\":1,\"unitPrice\":\"290.00\",\"currency\":\"EUR\",\"sku\":\"P00525485-3\"}]",
     "minicart_content":"<ul class=\"checkout-types minicart\">\n<li>",
     "cart_qty":"1",
     "added_product_json":"{\"id\":\"1675695\",\"size\":\"40\"}"
    }`)
    
        if err := json.Unmarshal(body, &j); err != nil {
            panic(err)
        }
    
        var k []productRow
        if err := json.Unmarshal([]byte(j.Addcartrows), &k); err != nil {
            panic(err)
        }
    
        var u addedProduct
        if err := json.Unmarshal([]byte(j.AddedProductJSON), &u); err != nil {
            panic(err)
        }
        fmt.Printf("%#v\n\n", j)
        fmt.Printf("%#v\n\n", k)
        fmt.Printf("%#v\n\n", u)
    }