Home > Software design >  Trying to get 2 integers out of a JSON subsection
Trying to get 2 integers out of a JSON subsection

Time:01-20

I'm using the VirusTotal HTTP API in Golang to get the votes on an URL, both malicious and harmless. I want to get them using structs, and then unmarshaling the URL using data from these structs. But when I try that, this error shows up: cannot convert "harmless" (untyped string constant) to int

The code I am currently using is: type Votes struct { harmless int malicious int }

type Attributes struct {
    votes []Votes `json:"total_votes"`
}

type data struct {
    attributes []Attributes `json:"attributes"`
}

type jsonstruct struct {
    data []data `json:"data"`
}

var printdata jsonstruct
fmt.Println(resp)
json.Unmarshal([]byte(body), &printdata)
fmt.Println(printdata.data[0].attributes[0].votes["harmless"])

And the part of the JSON that I want to get is:

{
"data": {
    "attributes": {
        "last_modification_date": 1642534694,
        "last_http_response_cookies": {
            "1P_JAR": "2022-01-18-19",
            "NID": "511=drq8-0Gwl0gpw2D-iyZhxrizpE--UMOyc_bO381XXkxknypvl_IETvsxRw3p8kMlBtiYEuSbASKK1wHirmgxce79kgzGMg9MryT0PnHox6kWbmEQTe2vsv_HtVZDFDXiLt4HKpcyDczOT8c_OK8bPb_P-f8rbIXJu_xrA0Ce4lw",
            "SameSite": "none"
        },
        "times_submitted": 82289,
        "total_votes": {
            "harmless": 54,
            "malicious": 18
        },

As you can see, I want to get the contents of the subsection total_votes, which are integers harmless and malicious. In short, how can I get them without getting the error about them being untyped strings?

CodePudding user response:

You need to define valid data structure that matches json structure. And after Unmarshal you can access votes fields as res.Data.Attributes.TotalVotes.Harmless . For example (https://go.dev/play/p/YCtV1u-KF7Y):

package main

import (
    "encoding/json"
    "fmt"
    "log"
)

type Result struct {
    Data struct {
        Attributes struct {
            LastHTTPResponseCookies struct {
                OnePJAR  string `json:"1P_JAR"`
                Nid      string `json:"NID"`
                SameSite string `json:"SameSite"`
            } `json:"last_http_response_cookies"`
            LastModificationDate float64 `json:"last_modification_date"`
            TimesSubmitted       float64 `json:"times_submitted"`
            TotalVotes           struct {
                Harmless  float64 `json:"harmless"`
                Malicious float64 `json:"malicious"`
            } `json:"total_votes"`
        } `json:"attributes"`
    } `json:"data"`
}

var input = `{
"data": {
    "attributes": {
        "last_modification_date": 1642534694,
        "last_http_response_cookies": {
            "1P_JAR": "2022-01-18-19",
            "NID": "pcyDczOT8c_OK8bPb_P-f8rbIXJu_xrA0Ce4lw",
            "SameSite": "none"
        },
        "times_submitted": 82289,
        "total_votes": {
            "harmless": 54,
            "malicious": 18
        }
    }
}
}
`

func main() {
    var res Result
    if err := json.Unmarshal([]byte(input), &res); err != nil {
        log.Fatal(err)
    }
    fmt.Printf("% v", res.Data.Attributes.TotalVotes.Harmless)
}
  •  Tags:  
  • Related