Read some json data without using struct [SOLVED]

Hey there, I got a json response from an API which my service integrates, I need only 1 data from all stuff from this json, searching on Google I found the solution below, e.g;

This code;

    // replace this by fetching actual response body
    responseBody := `{"textfield":"I'm a text.","num":1234,"list":[1,2,3]}`
    var data map[string]interface{}
    err := json.Unmarshal([]byte(responseBody), &data)
    if err != nil {
        panic(err)
    }
    fmt.Println(data["list"])
    fmt.Println(data["textfield"])

Reads the following json;

{"textfield":"I'm a text.","num":1234,"list":[1,2,3]}

In my situation I have the following json structure;

{"status":"sucesso","coderro":0,"erro":"","dados":[{"idboleto":"38106","datavencimento":"2017-11-10","datapagamento":"0000-00-00","codigodebarras":"...........................","urlfatura":".............","urlboleto":"THIS IS THE ONLY DATA THAT I NEED","dataemissao":"2017-09-12","valor":"70.00","valorpago":"0.00","meucodigo":"","cpfcnpj":"xxx.xxx.xxx-xx","nome":"...............................................","email":"","celular":"","fixo":"","status":"0"}]}

I need to get the “urlboleto” value, this data is inside an array, but the filter that I use always return only one array item, the structure is always the same as shown here.

How could I implement that code above to read this first item in “dados” array and the “urlboleto” attribute?

Tnx.

Is “without using struct” a strict requirement?

Using JSON to Go, I get a valid struct back:

type AutoGenerated struct {
	Status  string `json:"status"`
	Coderro int    `json:"coderro"`
	Erro    string `json:"erro"`
	Dados   []struct {
		Idboleto       string `json:"idboleto"`
		Datavencimento string `json:"datavencimento"`
		Datapagamento  string `json:"datapagamento"`
		Codigodebarras string `json:"codigodebarras"`
		Urlfatura      string `json:"urlfatura"`
		Urlboleto      string `json:"urlboleto"`
		Dataemissao    string `json:"dataemissao"`
		Valor          string `json:"valor"`
		Valorpago      string `json:"valorpago"`
		Meucodigo      string `json:"meucodigo"`
		Cpfcnpj        string `json:"cpfcnpj"`
		Nome           string `json:"nome"`
		Email          string `json:"email"`
		Celular        string `json:"celular"`
		Fixo           string `json:"fixo"`
		Status         string `json:"status"`
	} `json:"dados"`
}

If you cannot use a struct, you would need to work with a couple of type assertions - see this playground code

If even this does not match all of your use cases, consider using a decoder instead of unmarshalling.

6 Likes

Another option is github.com/tidwall/gjson. It has a dot notation path for quickly extracting specific or sets of values from JSON.

If that doesn’t float your boat, there are other packages that might.

4 Likes

Hey there I already solved the problem months ago, I just forgot to mark the title as solved. Really appreciate your help. Thank you guys.

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