Original post

I have some problem to convert JSON from API to static struct, here is my example JSON

{
    "layoutID": 1,
    "data": [
        {
            "widgetID": 1,
            "name": "widget 1",
            "type": "product",
            "data": [
                {
                    "productID": 1,
                    "name": "product 1"
                }
            ]
        },
        {
            "widgetID": 1,
            "name": "widget 2",
            "type": "voucher",
            "data": [
                {
                    "voucherID": 1,
                    "name": "voucher 1"
                }
            ]
        }
    ]
}

I want to create a structure for this JSON, but I have no idea to convert it. I will try to convert and cast with the interface by not show that.

Can you help me? Thanks

What have you tried so far and how is it not working?

@j-forster
I have been created structs like this

type Layout struct {
        LayoutID int      `json:"layoutID"`
        Data     []Widget `json:"data"`
}

type Widget struct {
        WidgetID int           `json:"widgetID"`
        Name     string        `json:"name"`
        Type     string        `json:"type"`
        Data     []interface{} `json:"data"`
}

type Product struct {
        ProductID int    `json:"productID"`
        Name      string `json:"name"`
}

type Voucher struct {
        VoucherID int    `json:"voucherID"`
        Name      string `json:"name"`
}

And I will try to run and create simulation from this JSON. Here is my code
https://play.golang.org/p/M8HTKWPEHqn

when I cast to struct, that will result false

why don’t you just unmarshal to the struct you created?

     var l Layout

        if err := json.Unmarshal(byt, &l); err != nil {
                panic(err)
        }

        fmt.Printf("layout: %+v",l)

https://play.golang.org/p/j8SchGKk2A5

Continuing the discussion from Try convert dynamic JSON to struct:

@bklimczak Thanks a lot for helping me, It’s really helped me. But, I’ve another problem after unmarshall, I’ve found data on Widget struct is a []interface{} type.

I’ve tried to cast these codes, but the result is error invalid type assertion. How can I do or any other way to cast into Product and Voucher Struct?

Here is my improvement code after checking.
https://play.golang.org/p/2zmGTlbC9pB

Thanks @bklimczak :+1:

In the loop you wrote, the Data is type widget. You don’t have to cast it. What you want to do is iterate over the Data in it https://play.golang.org/p/QoLQxyv1zKr

I see that, but Data on widget 1 is product struct such as I set ProductID, Name and Data on widget 2, not Product but also is Voucher struct, so why I cast it because on Data the other is not the same. So that why I set like this https://play.golang.org/p/2zmGTlbC9pB.

And I’ve found it, after unmarshall layout the result on widget 2 data have ProductID=0, not VoucherID=1.

type Widget struct {
        WidgetID int           `json:"widgetID"`
        Name     string        `json:"name"`
        Type     string        `json:"type"`
        Data     []interface{} `json:"data"`
}

Because Data on Widget is dynamic, so that I set type []interface{}, not only Product.

Thanks @bklimczak

http://mholt.github.io/json-to-go/

Thanks @eudore.

I’v tried this, but result not resolved, this is the result

type AutoGenerated struct {
        LayoutID int `json:"layoutID"`
        Data     []struct {
                WidgetID int    `json:"widgetID"`
                Name     string `json:"name"`
                Type     string `json:"type"`
                Data     []struct {
                        ProductID int    `json:"productID"`
                        Name      string `json:"name"`
                } `json:"data"`
        } `json:"data"`
}

I think you can find your answer here: http://mattyjwilliams.blogspot.com/2013/01/using-go-to-unmarshal-json-lists-with.html
If you have any more questions – let us know.

https://mholt.github.io/json-to-go/
https://blog.golang.org/json

You can also define your receiving object as map[string]interface{}, and the JSON unmarshal will fill in all the details, recursively.

Just my 2 cents: https://play.golang.org/p/Dh3bYSqJjLD

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.