Search code examples
jsongospreedly

Parsing JSON in GoLang into struct


So, I'm having some trouble parsing this data in golang:

{
"gateways": [
    {
        "token": "my_token_here",
        "gateway_type": "test",
        "description": null,
        "payment_methods": [
            "credit_card",
            "sprel",
            "third_party_token",
            "bank_account",
            "apple_pay"
        ],
        "state": "retained",
        "created_at": "2016-03-12T18:52:37Z",
        "updated_at": "2016-03-12T18:52:37Z",
        "name": "Spreedly Test",
        "characteristics": [
            "purchase",
            "authorize",
            "capture",
            "credit",
            "general_credit",
            "void",
            "verify",
            "reference_purchase",
            "purchase_via_preauthorization",
            "offsite_purchase",
            "offsite_authorize",
            "3dsecure_purchase",
            "3dsecure_authorize",
            "store",
            "remove",
            "disburse",
            "reference_authorization"
        ],
        "credentials": [],
        "gateway_specific_fields": [],
        "redacted": false
    }
]

}

When using this struct I can get it to output pretty easily.

type gateways struct {
    Gateways []struct {
        Characteristics       []string      `json:"characteristics"`
        CreatedAt             string        `json:"created_at"`
        Credentials           []interface{} `json:"credentials"`
        Description           interface{}   `json:"description"`
        GatewaySpecificFields []interface{} `json:"gateway_specific_fields"`
        GatewayType           string        `json:"gateway_type"`
        Name                  string        `json:"name"`
        PaymentMethods        []string      `json:"payment_methods"`
        Redacted              bool          `json:"redacted"`
        State                 string        `json:"state"`
        Token                 string        `json:"token"`
        UpdatedAt             string        `json:"updated_at"`
    } `json:"gateways"` 
}

But as soon as I seperate the "Gateways []struct" into its own struct then it returns an empty array...

Full source.

type gateway struct {  
  Characteristics       []string      `json:"characteristics"`
  CreatedAt             string        `json:"created_at"`
  Credentials           []interface{} `json:"credentials"`
  Description           interface{}   `json:"description"`
  GatewaySpecificFields []interface{} `json:"gateway_specific_fields"`
  GatewayType           string        `json:"gateway_type"`
  Name                  string        `json:"name"`
  PaymentMethods        []string      `json:"payment_methods"`
  Redacted              bool          `json:"redacted"`
  State                 string        `json:"state"`
  Token                 string        `json:"token"`
  UpdatedAt             string        `json:"updated_at"`
}
type gateways struct {
  Gateways []gateway `json:"gateways"`
}

func ParseResponse() {
  var parsed gateways
  json.Unmarshal(json, &parsed)
}

Solution

  • There's a problem with your ParseResponse function, you're calling json.Unmarshal passing as first parameter json, that's a packge name: that's ambiguous.

    As you can see, your code works well changing the ParseResponse function.

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    type gateway struct {
        Characteristics       []string      `json:"characteristics"`
        CreatedAt             string        `json:"created_at"`
        Credentials           []interface{} `json:"credentials"`
        Description           interface{}   `json:"description"`
        GatewaySpecificFields []interface{} `json:"gateway_specific_fields"`
        GatewayType           string        `json:"gateway_type"`
        Name                  string        `json:"name"`
        PaymentMethods        []string      `json:"payment_methods"`
        Redacted              bool          `json:"redacted"`
        State                 string        `json:"state"`
        Token                 string        `json:"token"`
        UpdatedAt             string        `json:"updated_at"`
    }
    
    type gateways struct {
        Gateways []gateway `json:"gateways"`
    }
    
    func ParseResponse(js []byte) {
        var parsed gateways
        json.Unmarshal(js, &parsed)
        fmt.Println(parsed)
    }
    
    func main() {
        var js []byte = []byte(`{
    "gateways": [
        {
            "token": "my_token_here",
            "gateway_type": "test",
            "description": null,
            "payment_methods": [
                "credit_card",
                "sprel",
                "third_party_token",
                "bank_account",
                "apple_pay"
            ],
            "state": "retained",
            "created_at": "2016-03-12T18:52:37Z",
            "updated_at": "2016-03-12T18:52:37Z",
            "name": "Spreedly Test",
            "characteristics": [
                "purchase",
                "authorize",
                "capture",
                "credit",
                "general_credit",
                "void",
                "verify",
                "reference_purchase",
                "purchase_via_preauthorization",
                "offsite_purchase",
                "offsite_authorize",
                "3dsecure_purchase",
                "3dsecure_authorize",
                "store",
                "remove",
                "disburse",
                "reference_authorization"
            ],
            "credentials": [],
            "gateway_specific_fields": [],
            "redacted": false
        }
    ]
    }`)
        /*
            var parsed gateways
            e := json.Unmarshal(js, &parsed)
            if e != nil {
                fmt.Println(e.Error())
            } else {
                fmt.Println(parsed)
            }
        */
        ParseResponse(js)
    }
    

    Outputs:

    {[{[purchase authorize capture credit general_credit void verify reference_purchase purchase_via_preauthorization offsite_purchase offsite_authorize 3dsecure_purchase 3dsecure_authorize store remove disburse reference_authorization] 2016-03-12T18:52:37Z [] <nil> [] test Spreedly Test [credit_card sprel third_party_token bank_account apple_pay] false retained my_token_here 2016-03-12T18:52:37Z}]}