How to read json from a url and decode it and store a data in a variable

Hello we get data from a url in json format.
like :
curl -s http://mydata.mygolang.com/getstatus.php
{“status”:“success”,“brand_name”:“NO”,“domain_name”:“NO”,“expire_date”:“2021-05-21”}
This is the data we get.
Now i want to store status, brand_name, domain_name and expiry date in different variables.
Status in status variable, brand_name in brand variable and so on.
I didn’t find any solution, anyone here to help in this case ?

Well, you can decode to a specifiic struct or a map of interface.
For example

func main() {
	byt := []byte(`{"status":"success","brand_name":"NO","domain_name":"NO","expire_date":"2021-05-21"}`)

	// Using a struct
	var dt Autogenerated
	if err := json.Unmarshal(byt, &dt); err == nil {
		fmt.Println(dt.Status)
	}

	// Using a map
	var dat map[string]interface{}
	if err := json.Unmarshal(byt, &dat); err == nil {
		fmt.Println(dat["status"])
	}
}

you can use this page https://mholt.github.io/json-to-go/ to convert a json to golang struct

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