JSON missing pieces

Hi, I have a piece of code that retrieves data in a json format but for some reason he shows me everything but… the publish_date and update_date information, i suspect maybe something to do with my type but well if anyone can help:

package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
)

type Cve struct {
	Cve_id        string
	Cwe_id        int
	Summary       string
	Exploit_count int
	publish_date  string
	update_date   string
	url           string
}

func main() {

	//get cve list
	res, err := http.Get("http://www.cvedetails.com/json-feed.php?numrows=10&vendor_id=0&product_id=0&version_id=0&hasexp=0&opec=0&opov=0&opcsrf=0&opfileinc=0&opgpriv=0&opsqli=0&opxss=0&opdirt=0&opmemc=0&ophttprs=0&opbyp=0&opginf=0&opdos=0&orderby=3&cvssscoremin=5")
	if err != nil {
		log.Fatal(err)
	}

	//read the results to a variable
	data, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		log.Fatal(err)
	}

	var cves []Cve

	err = json.Unmarshal(data, &cves)

	for i := 0; i < len(cves); i++ {
		fmt.Println(cves[i].Cve_id)
		fmt.Println(cves[i].Exploit_count)
		fmt.Println(cves[i].Summary)
		fmt.Println(string(cves[i].publish_date))
	}
}

Output

CVE-2016-5821
0
Huawei HiSuite before 4.0.4.204_ove (Out of China) and before 4.0.4.301 (China) use a weak ACL (FILE_WRITE_DATA for BUILTIN\Users) for the HiSuite service directory, which allows local users to gain SYSTEM privileges via a Trojan horse (1) SspiCli.dll or (2) USERENV.dll file or possibly other unspecified DLL files.

➜ cve git:(master) ✗

Fields must be exported (name starts with a capital letter) for the JSON package to be able to access them. Underscores are also not customary in Go and you can use the json struct tag to tell the decoder what field to expect, so your struct should look something like

type CVE struct {
	CVEID         string `json:"cve_id"`
	CWEID         int    `json:"cwe_id"`
	Summary       string
	ExploitCount  int    `json:"exploit_count"`
	PublishDate   string `json:"publish_date"`
	UpdateDate    string `json:"update_date"`
	URL           string
}

hmm that worked thanks for the explanation calmh

1 Like

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