Parse Json Value not work

Hi I am currently doing a project and I am not sure what am I wrong here. I want to print just URL from the response

Here is the example json response but I want to print only the value of URL . When i try to edit in code like “page.url”. It is not work.

 "page": {
        "country": "US",
        "server": "cloudflare",
        "domain": "www.hackerone.com",
        "ip": "2606:4700::6810:6334",
        "mimeType": "text/html",
        "asnname": "CLOUDFLARENET, US",
        "asn": "AS13335",
        "url": "https://www.hackerone.com/?mkt_tok=MTY4LU5BVS03MzIAAAF-rKuriE5nHtVheqeA84AIT2tevNUnsoT9TxntfJhmpZiMVBpnp-OsEoZ0SVNYn5ZrrhGGU_54HmE3O5XSFyxa5ZDkja-Yt60twui1SRYW8m0",
        "status": "200"
      },

Below are my codes

package main

import(

    "fmt"

    "net/http"

    "io/ioutil"

    "encoding/json"

)

type Data []struct {

    Url string `json:"page,url"`

}

func main() {

    url := "https://urlscan.io/api/v1/search/?q=hackerone.com"

    client := &http.Client{}

    req, err := http.NewRequest("GET" , url , nil)

    req.Header.Set("Content-Type", "application/json")

    req.Header.Set("API-Key", "")

    resp, err := client.Do(req)

    if err != nil {

        fmt.Println(err)

    }

    defer resp.Body.Close()

    body, _ := ioutil.ReadAll(resp.Body)

    fmt.Println(string(body)) //this will print all the output

    var u Data

    err = json.Unmarshal(body, &u)

    f  := make([]string, 0)

    for _, u := range u {

        f  = append(f,u.Url)

    }

    fmt.Println(f)

}

Hi @osibyte,

Maybe a simple reason - json:"page,url" contains a comma, I guess this should read json:"page.url".

I already tried that not work.

You need correct Go struct which represents the json data.

type Data struct {
	Results []struct {
		IndexedAt time.Time `json:"indexedAt"`
		Task      struct {
			Visibility string    `json:"visibility"`
			Method     string    `json:"method"`
			Domain     string    `json:"domain"`
			Time       time.Time `json:"time"`
			UUID       string    `json:"uuid"`
			URL        string    `json:"url"`
			Tags       []string  `json:"tags"`
		} `json:"task"`
		Stats struct {
			UniqIPs           int `json:"uniqIPs"`
			ConsoleMsgs       int `json:"consoleMsgs"`
			UniqCountries     int `json:"uniqCountries"`
			DataLength        int `json:"dataLength"`
			EncodedDataLength int `json:"encodedDataLength"`
			Requests          int `json:"requests"`
		} `json:"stats"`
		Page struct {
			Country  string `json:"country"`
			Server   string `json:"server"`
			Domain   string `json:"domain"`
			IP       string `json:"ip"`
			MimeType string `json:"mimeType"`
			Asnname  string `json:"asnname"`
			Asn      string `json:"asn"`
			URL      string `json:"url"`
			Status   string `json:"status"`
		} `json:"page"`
		ID         string        `json:"_id"`
		Sort       []interface{} `json:"sort"`
		Result     string        `json:"result"`
		Screenshot string        `json:"screenshot"`
	} `json:"results"`
	Total   int  `json:"total"`
	Took    int  `json:"took"`
	HasMore bool `json:"has_more"`
}
1 Like

Here you have the whole code with few more fixes :slightly_smiling_face:

package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"time"

	"net/http"
)

type Data struct {
	Results []struct {
		IndexedAt time.Time `json:"indexedAt"`
		Task      struct {
			Visibility string    `json:"visibility"`
			Method     string    `json:"method"`
			Domain     string    `json:"domain"`
			Time       time.Time `json:"time"`
			UUID       string    `json:"uuid"`
			URL        string    `json:"url"`
			Tags       []string  `json:"tags"`
		} `json:"task"`
		Stats struct {
			UniqIPs           int `json:"uniqIPs"`
			ConsoleMsgs       int `json:"consoleMsgs"`
			UniqCountries     int `json:"uniqCountries"`
			DataLength        int `json:"dataLength"`
			EncodedDataLength int `json:"encodedDataLength"`
			Requests          int `json:"requests"`
		} `json:"stats"`
		Page struct {
			Country  string `json:"country"`
			Server   string `json:"server"`
			Domain   string `json:"domain"`
			IP       string `json:"ip"`
			MimeType string `json:"mimeType"`
			Asnname  string `json:"asnname"`
			Asn      string `json:"asn"`
			URL      string `json:"url"`
			Status   string `json:"status"`
		} `json:"page"`
		ID         string        `json:"_id"`
		Sort       []interface{} `json:"sort"`
		Result     string        `json:"result"`
		Screenshot string        `json:"screenshot"`
	} `json:"results"`
	Total   int  `json:"total"`
	Took    int  `json:"took"`
	HasMore bool `json:"has_more"`
}

func main() {

	url := "https://urlscan.io/api/v1/search/?q=hackerone.com"

	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("API-Key", "")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	defer resp.Body.Close()

	body, _ := ioutil.ReadAll(resp.Body)

	var u Data

	err = json.Unmarshal(body, &u)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	f := make([]string, len(u.Results))

	for _, r := range u.Results {
		f = append(f, r.Page.URL)
		fmt.Println(r.Page.URL)
		fmt.Println(r.Task.URL)
	}
}
1 Like

Dammm Man, I don’t even know how to appreciate you. I was sticking at this for over two months.

Thank you so much.