Loop over nested JSON elements in Go -
i receiving json response api contains 1 or more "entities". json looks this:
{ "3211": { "entity_id": "3211", "status": "complete", "coupon_code": "coupon", "shipping_description": "shipping - au courier", "customer_id": "2775", "base_discount_amount": "-50.0000", "base_grand_total": "149.0000", "base_shipping_amount": "0.0000", "base_shipping_tax_amount": "0.0000", "base_subtotal": "199.0000", "base_tax_amount": "0.0000", "base_total_paid": "149.0000", "base_total_refunded": null, "discount_amount": "-50.0000", "grand_total": "149.0000", "shipping_amount": "0.0000", "shipping_tax_amount": "0.0000", "store_to_order_rate": "1.0000", "subtotal": "199.0000", "tax_amount": "0.0000", "total_paid": "149.0000", "total_refunded": null, "base_shipping_discount_amount": "0.0000", "base_subtotal_incl_tax": "199.0000", "base_total_due": "0.0000", "shipping_discount_amount": "0.0000", "subtotal_incl_tax": "199.0000", "total_due": "0.0000", "increment_id": "200000423", "base_currency_code": "aud", "discount_description": "coupon", "remote_ip": "123.123.123.123", "store_currency_code": "aud", "store_name": "australia", "created_at": "2017-07-17 03:07:40", "shipping_incl_tax": "0.0000", "payment_method": "ewayrapid_ewayone", "gift_message_from": null, "gift_message_to": null, "gift_message_body": null, "tax_name": null, "tax_rate": null, "addresses": [ { "region": "south australia", "postcode": "5000", "lastname": "doe", "street": "level 6\n25 example street", "city": "adelaide", "email": "example@email.com", "telephone": "+61 123 456 789", "country_id": "au", "firstname": "john", "address_type": "billing", "prefix": null, "middlename": null, "suffix": null, "company": null }, { "region": "south australia", "postcode": "5000", "lastname": "doe", "street": "level 6\n25 example street", "city": "adelaide", "email": "example@email.com", "telephone": "+61 123 456 789", "country_id": "au", "firstname": "john", "address_type": "shipping", "prefix": null, "middlename": null, "suffix": null, "company": null } ] } }
i have written following struct
types:
type orders map[string]order type order struct { entityid string `json:"entity_id"` status string `json:"status"` couponcode string `json:"coupon_code"` shippingdescription string `json:"shipping_description"` customerid string `json:"customer_id"` basediscountamount string `json:"base_discount_amount"` basegrandtotal string `json:"base_grand_total"` baseshippingamount string `json:"base_shipping_amount"` baseshippingtaxamount string `json:"base_shipping_tax_amount"` basesubtotal string `json:"base_subtotal"` basetaxamount string `json:"base_tax_amount"` basetotalpaid string `json:"base_total_paid"` basetotalrefunded string `json:"base_total_refunded"` discountamount string `json:"discount_amount"` grandtotal string `json:"grand_total"` shippingamount string `json:"shipping_amount"` shippingtaxamount string `json:"shipping_tax_amount"` storetoorderrate string `json:"store_to_order_rate"` subtotal string `json:"subtotal"` taxamount string `json:"tax_amount"` totalpaid string `json:"total_paid"` totalrefunded string `json:"total_refunded"` baseshippingdiscountamount string `json:"base_shipping_discount_amount"` basesubtotalincltax string `json:"base_subtotal_incl_tax"` basetotaldue string `json:"base_total_due"` shippingdiscountamount string `json:"shipping_discount_amount"` subtotalincltax string `json:"subtotal_incl_tax"` totaldue string `json:"total_due"` incrementid string `json:"increment_id"` basecurrencycode string `json:"base_currency_code"` discountdescription string `json:"discount_description"` remoteip string `json:"remote_ip"` storecurrencycode string `json:"store_currency_code"` storename string `json:"store_name"` createdat string `json:"created_at"` shippingincltax string `json:"shipping_incl_tax"` paymentmethod string `json:"payment_method"` taxname string `json:"tax_name"` taxrate string `json:"tax_rate"` addresses map[string]orderaddresses `json:"addresses"` } type orderaddresses []struct { region string `json:"region"` postcode string `json:"postcode"` lastname string `json:"lastname"` street string `json:"street"` city string `json:"city"` email string `json:"email"` telephone string `json:"telephone"` countryid string `json:"country_id"` firstname string `json:"firstname"` addresstype string `json:"address_type"` prefix *string `json:"prefix"` middlename *string `json:"middlename"` suffix *string `json:"suffix"` company *string `json:"company"` }
i trying process (the getfromordersapi(page)
function returns json mentioned above in orders
type):
for page := 1; page < 3; page++ { orders := getfromordersapi(page) _, order := range orders { //process order items except addresses fmt.println("processing entity:", orders.entityid) _, orderaddress := range order.addresses { //trying access address values - example below fmt.println(orderaddress.region) } } }
when running this, error:
orderaddress.region undefined (type orderaddresses has no field or method region)
where going wrong?
the error getting due way defining orderaddresses
struct. instead of
type orderaddresses []struct {
if use
type orderaddresses struct {
without braces, compile error should go away.
that being said, i'm not sure if json parse expect given blob i'm looking at. appears addresses
in json blob array. means instead of representing as
addresses map[string]orderaddresses `json:"addresses"`
you should make slice of orderaddresses
addresses []orderaddresses `json:"addresses"`
Comments
Post a Comment