I am trying to get access to object's values inside of array
[
{
"name": "London",
"lat": 51.5073219,
"lon": -0.1276474,
"country": "GB",
"state": "England"
}
]
I use this code to unmarshal it
content, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
var data []ResponseData
err = json.Unmarshal(content, &data)
if err != nil {
log.Fatal(err)
}
This is my struct
type ResponseData struct {
Name string `json:"name"`
Lat float32 `json:"lat"`
Lon float32 `json:"lon"`
Country string `json:"country"`
State string `json:"state"`
}
I need to simply fmt.Println(data.Lat, data.Lon) later.
The code you presented should unmarshal your JSON successfully; the issue is with the way you are trying to use the result. You say you want to use fmt.Println(data.Lat, data.Lon) but this will not work because data is a slice ([]ResponseData) not a ResponseData. You could use fmt.Println(data[0].Lat, data[0].Lon) (after checking the number of elements!) or iterate through the elements.
The below might help you experiment (playground - this contains a little more content than below):
package main
import (
"encoding/json"
"fmt"
"log"
)
const rawJSON = `[
{
"name": "London",
"lat": 51.5073219,
"lon": -0.1276474,
"country": "GB",
"state": "England"
}
]`
type ResponseData struct {
Name string `json:"name"`
Lat float32 `json:"lat"`
Lon float32 `json:"lon"`
Country string `json:"country"`
State string `json:"state"`
}
func main() {
var data []ResponseData
err := json.Unmarshal([]byte(rawJSON), &data)
if err != nil {
log.Fatal(err)
}
if len(data) == 1 { // Would also work for 2+ but then you are throwing data away...
fmt.Println("test1", data[0].Lat, data[0].Lon)
}
for _, e := range data {
fmt.Println("test2", e.Lat, e.Lon)
}
}
Related
I am new to GoLang, and have a question about filling an array from nested JSON data. I have looked through Stack overflow yesterday and cannot find this exact topic, only threads that are similar, but do not provide a direct solution.
Lets say I have some nested JSON data like what is given below:
How can I create a nested struct to fill an array of the close prices. My code is given below.
My goal is to have an array where, arr = {157.92, 142.19, 148.26}
Thanks in advance! I greatly appreciate any help!
{
"history": {
"day": [
{
"date": "2019-01-02",
"open": 154.89,
"high": 158.85,
"low": 154.23,
"close": 157.92,
"volume": 37039737
},
{
"date": "2019-01-03",
"open": 143.98,
"high": 145.72,
"low": 142.0,
"close": 142.19,
"volume": 91312195
},
{
"date": "2019-01-04",
"open": 144.53,
"high": 148.5499,
"low": 143.8,
"close": 148.26,
"volume": 58607070
}
...
]
}
}
// DATA STRUCTURE
type Hist struct {
History string `json:"history"`
}
type Date struct {
Day string `json:"day"`
}
type Price struct {
Close []string `json:"close"`
}
// HISTORICAL QUOTES
func get_quotes(arg1 string, arg2 string, arg3 string, arg4 string) []string {
// arg1 = ticker symbol, arg2 = start, arg3 = end, arg4 = access token
// TRADIER API
apiUrl := "https://sandbox.tradier.com/v1/markets/history?symbol=" + arg1 + "&interval=daily&start=" + arg2 + "&end=" + arg3
u, _ := url.ParseRequestURI(apiUrl)
urlStr := u.String()
client := &http.Client{}
r, _ := http.NewRequest("GET", urlStr, nil)
r.Header.Add("Authorization", "Bearer "+arg4)
r.Header.Add("Accept", "application/json")
resp, _ := client.Do(r)
responseData, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.Status)
fmt.Println(string(responseData))
var response Price
json.NewDecoder(resp.Body).Decode(&response)
fmt.Println(response.Close)
return response.Close
}
Something like this should give you what you need. Your data structure does not correctly reflect the response from the API. I used this tool to quickly convert the JSON value into a Go struct type. Once you're decoding the response correctly, then it's just a matter of iterating over each Day struct and appending the close value to an output array.
I added the core stuff for decoding and mapping the values. You can handle customizing the client, request, and headers however you'd like by combining what you've already got.
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
type Response struct {
History History `json:"history"`
}
type Day struct {
Date string `json:"date"`
Open float64 `json:"open"`
High float64 `json:"high"`
Low float64 `json:"low"`
Close float64 `json:"close"`
Volume int `json:"volume"`
}
type History struct {
Day []Day `json:"day"`
}
func main() {
prices, err := closePrices()
if err != nil {
log.Fatal(err)
}
fmt.Println(prices)
}
func closePrices() (out []float64, err error) {
resp, err := http.Get("...")
if err != nil {
return
}
r := Response{}
err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return
}
for _, d := range r.History.Day {
out = append(out, d.Close)
}
return
}
I am trying to format my response from a server in an array of multiple object JSON. I tried slice and []Struct but I believe I am not able to understand their usage correctly.
I tried the method mentioned here - https://play.golang.org/p/9OEPzbf0Me0 but didn't work in my case
I am using golang to get response for a query parameter
router.HandleFunc("/v1.0/singles", GetInfo).Methods("GET").Queries("Group", "{group}")
//controller.go
func SendToServer(jsonValue []byte, command string) (models.JSON, error) {
var pdatar models.JSON
s := []string{"http://172.xx.xxx.xx:xxxxx/", command}
response, err := http.Post(strings.Join(s, ""), "application/json", bytes.NewBuffer(jsonValue))
data, err := ioutil.ReadAll(response.Body)
err = json.Unmarshal(data, &pdatar)
mdatar := make(models.JSON)
mdatar["ID"] = pdatar["id"]
mdatar["Name"] = pdatar["name"]
fmt.Println(mdatar) //This is how it is coming map[ID:[877,235], Name:[FCU, GHP]]
/*Another attempt
var temp models.Arr
json.Unmarshal(data, &temp)
fmt.Println(temp) ---- returning null
*/
return mdatar, nil
}
func (c Controller) GetSingle(pdata models.Single) (models.JSON, error) {
mdata := make(models.JSON)
mdata["group"] = pdata.Group
jsonValue, err := json.Marshal(mdata)
pdatar, err := SendToServer(jsonValue, "func_info")
return pdatar, nil
// json.NewEncoder(w).Encode(pdatar) //sending this to my main function
}
//model.go
type JSON map[string]interface{}
type Single struct {
ID int `json:"id"`
Name string `json:"name"`
}
type Data []Single
type Arr [][]string
I have a response from my API in a format
{
"id":[877,235,312,429],
"name" ["FCU","GHP","c","d"],
"group":["x","x","y","z"]
}
With Current Code I am receiving a response
{"id":[877 235], "name": ["FCU" "GHP"]
Though I am expecting a response
{
"data":
[
{"id": 877
"name" : "FCU"
}
{"id": 235
"name": "GHP"
}
]
}
Your mdatarr doesn't change the format any, so this result makes sense.
If the data received from the server is in the format:
{
"id": [877,235,312,429],
"name" ["FCU","GHP","c","d"],
"group": ["x","x","y","z"]
}
then you will need translate it. Ideally you'd fix the server so that it sends data in the format:
{
{
"id": 877,
"name": "FCU",
"group": "x",
},
...
}
If the server could send something like the above, then you could simply unmarshal it into a models.Data object.
If that is not an option, and you really need to do the translation client-side, then you'll need a for loop that does something like this:
ids := pdatarr["id"]
names := pdatarr["name"]
...
if len(ids) != len(names) {
return nil, errors.New("Invalid input format")
}
var mdatar models.Data
for i := range(ids) {
mdatar = append(mdatar, models.Single{ids[i], names[i], ...})
}
return models.JSON{"data": mdatar}, nil
My input json data is this (cannot be changed, from an external resource):
[{
"Url": "test.url",
"Name": "testname"
},{
"FormName": "Test - 2018",
"FormNumber": 43,
"FormSlug": "test-2018"
}]
I have two structs that will always match the data within the array:
type UrlData struct{
"Url" string `json:Url`
"Name" string `json:Name`
}
type FormData struct{
"FormName" string `json:FormName`
"FormNumber" string `json:FormNumber`
"FormSlug" string `json:FormSlug`
}
Obviously the code below will not work, but is it possible at the top level (or otherwise) to declare something like this:
type ParallelData [
urlData UrlData
formData FormData
]
Use a two step process for unmarshaling. First, unmarshal a list of arbitrary JSON, then unmarshal the first and second element of that list into their respective types.
You can implement that logic in a method called UnmarshalJSON, thus implementing the json.Unmarshaler interface. This will give you the compound type you are looking for:
type ParallelData struct {
UrlData UrlData
FormData FormData
}
// UnmarshalJSON implements json.Unmarshaler.
func (p *ParallelData) UnmarshalJSON(b []byte) error {
var records []json.RawMessage
if err := json.Unmarshal(b, &records); err != nil {
return err
}
if len(records) < 2 {
return errors.New("short JSON array")
}
if err := json.Unmarshal(records[0], &p.UrlData); err != nil {
return err
}
if err := json.Unmarshal(records[1], &p.FormData); err != nil {
return err
}
return nil
}
Try it on the playground: https://play.golang.org/p/QMn_rbJj-P-
I think Answer of Peter is awesome.
Option 1:
type ParallelData [
urlData UrlData
formData FormData
]
if you need above structure then you can define it as
type UrlData struct {
Url string `json:"Url,omitempty"`
Name string `json:"Name,omitempty"`
}
type FormData struct {
FormName string `json:"FormName,omitempty"`
FormNumber string `json:"FormNumber,omitempty"`
FormSlug string `json:"FormSlug,omitempty"`
}
type ParallelData struct {
UrlData UrlData `json:"UrlData,omitempty"`
FormData FormData `json:"FormData,omitempty"`
}
In this case, your json will look like
[
{
"UrlData":{
"Url":"test.url",
"Name":"testname"
}
},
{
"FormData":{
"FormName":"Test - 2018",
"FormNumber":"43",
"FormSlug":"test-2018"
}
}
]
Option 2:
You've provide following json:
[
{
"Url":"test.url",
"Name":"testname"
},
{
"FormName":"Test - 2018",
"FormNumber":43,
"FormSlug":"test-2018"
}
]
If your json really look like, then you can use following struct
type UrlData struct {
Url string `json:Url`
Name string `json:Name`
}
type FormData struct {
FormName string `json:FormName`
FormNumber int `json:FormNumber`
FormSlug string `json:FormSlug`
}
type ParallelData struct {
UrlData
FormData
}
For both options, you can Unmarshall your json like this
var parallelData []ParallelData
err := json.Unmarshal([]byte(str), ¶llelData)
if err != nil {
panic(err)
}
fmt.Println(parallelData)
See option 1 in playground
See option 2 in playground
You can unmarshal into a map[string]interface{} for example:
type ParallelData map[string]interface{}
func main() {
textBytes := []byte(`[
{
"Url": "test.url",
"Name": "testname"
},
{
"FormName": "Test - 2018",
"FormNumber": 43,
"FormSlug": "test-2018"
}]`)
var acc []ParallelData
json.Unmarshal(textBytes, &acc)
fmt.Printf("%+v", acc)
}
Output:
=> [map[Url:test.url Name:testname] map[FormName:Test - 2018 FormNumber:43 FormSlug:test-2018]]
Playground
How to unmarshal the json and fill into structures. Like i'm having salesorder and salesorderdetails structures. In json i will have 1 record for salesorder and multiple items for salesorderdetails structure.
Here is the go code i have tried with single item and for multiple items, but working only for single record for salesorderdetails structure.
Gocode:
package main
import (
"encoding/json"
"fmt"
)
type Order struct {
SalesId string `json:"sales_id"`
Customer string `json:"customer_name"`
TotalPrice string `json:"totalprice"`
}
type OrderDetails struct {
DetailId string `json:"detail_id"`
SalesId string `json:"sales_id"`
ItemName string `json:"item_name"`
Qty string `json:"qty"`
Price string `json:"price"`
}
type Temp struct {
Salesorder Order `json:"Salesorder"`
Salesorderdetails OrderDetails `json:"OrderDetails"`
}
func main() {
jsonByteArray := []byte(`[{"Salesorder":{"sales_id":"SOO1","customer_name":"CUST1","totalprice":"200"}, "OrderDetails":{"detailid":"1","sales_id":"SOO1","item_name":"ITEM1","qty":"2","price":"100"}}]`)
//if i use above json it is working and if i use below json its not working
//jsonByteArray := []byte(`[{"Salesorder":{"sales_id":"SOO1","customer_name":"CUST1","totalprice":"200"}, "OrderDetails":{"detailid":"1","sales_id":"SOO1","item_name":"ITEM1","qty":"2","price":"100"},{"detailid":"2","sales_id":"SOO2","item_name":"ITEM2","qty":"3","price":"200"}}]`)
var temp []Temp
err := json.Unmarshal(jsonByteArray, &temp)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", temp)
fmt.Printf("%+v\n ", temp[0].Salesorder.SalesId)
}
Error while using multiple items:
panic: invalid character '{' looking for beginning of object key string
Output while using single item with success:
[{Salesorder:{SalesId:SOO1 Customer:CUST1 TotalPrice:200} Salesorderdetails:{SalesId:SOO1 ItemName:ITEM1 Qty:2 Price:100}}]
SOO1
Fiddle: updated with key in salesorderdetails
https://play.golang.org/p/klIAoNi18r
What you are tying to decode is not valid JSON. This would be valid:
{
"Salesorder": {
"sales_id": "SOO1",
"customer_name": "CUST1",
"totalprice": "200"
},
"OrderDetails": {
"sales_id": "SOO1",
"item_name": "ITEM1",
"qty": "2",
"price": "100"
}
}
But what you are giving is this:
{
"Salesorder": {
"sales_id": "SOO1",
"customer_name": "CUST1",
"totalprice": "200"
},
"OrderDetails": {
"sales_id": "SOO1",
"item_name": "ITEM1",
"qty": "2",
"price": "100"
},
{ // Things become invalid here
"sales_id": "SOO2",
"item_name": "ITEM2",
"qty": "3",
"price": "200"
}
}
It looks like you are trying to give a list of objects, in which case you need to wrap those objects in square brackets [] or you will have to give the second OrderDetails object its own key.
Try this:
package main
import (
"encoding/json"
"fmt"
)
type Order struct {
SalesId string `json:"sales_id"`
Customer string `json:"customer_name"`
TotalPrice string `json:"totalprice"`
}
type OrderDetails struct {
SalesId string `json:"sales_id"`
ItemName string `json:"item_name"`
Qty string `json:"qty"`
Price string `json:"price"`
}
type Temp struct {
Salesorder Order `json:"Salesorder"`
Salesorderdetails []OrderDetails `json:"OrderDetails"`
}
func main() {
jsonByteArray := []byte(`[{"Salesorder":{"sales_id":"SOO1","customer_name":"CUST1","totalprice":"200"}, "OrderDetails":[{"sales_id":"SOO1","item_name":"ITEM1","qty":"2","price":"100"},{"sales_id":"SOO2","item_name":"ITEM2","qty":"3","price":"200"}]}]`)
var temp []Temp
err := json.Unmarshal(jsonByteArray, &temp)
if err != nil {
panic(err)
}
//fmt.Printf("%+v\n", temp)
// Printing all Orders with some details
for _, order := range temp {
fmt.Println("Customer:", order.Salesorder.Customer)
for _, details := range order.Salesorderdetails {
fmt.Printf(" ItemName: %s, Price: %s\n", details.ItemName, details.Price)
}
}
}
.. OrderDetails is an array now
I am new to Go. I am working with a weather API. I have commented out the sections that cause the error. I have seen several other links that have a similar problem, however none of them seem to have the array in the middle of the JSON string. I'm sure there is a way to define the struct with a slice. I can't seem to get get the syntax to allow it. Here is where I'm stuck:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
// WeatherData struct to collect data from the API call
type WeatherData struct {
Wind Wind
Sys Sys
// Weather Weather
Name string `json:"name"`
}
////////////// ERROR when unmarshalling this struct /////////
// Weather provides basic weather info
// type Weather struct {
// ID int `json:"id"`
// Descrip string `json:"description"`
// Icon string `json:"icon"`
// }
/////////////////////////////////////////////////////////////
// Sys includes sunrise, sunset, country, etc.
type Sys struct {
Country string `json:"country"`
}
// Wind struct to get specific wind characteristics
type Wind struct {
Speed float64 `json:"speed"`
Degree float64 `json:"deg"`
Gust float64 `json:"gust"`
}
func main() {
res, getErr := http.Get("http://api.openweathermap.org/data/2.5/weather?zip=REMOVED,us&appid=REMOVEDBUTWILLPOSTJSONData")
if getErr != nil {
log.Fatalln("http.Get error: ", getErr)
}
defer res.Body.Close()
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatalln("Read Error: ", readErr)
}
//////////// UNABLE TO UNMARSHAL the array that passes through here ////
var data WeatherData
if err := json.Unmarshal(body, &data); err != nil {
panic(err)
}
fmt.Println("Wind gusts: ", data.Wind.Gust)
fmt.Println("Wind speed: ", data.Wind.Speed)
fmt.Println("Wind degrees: ", data.Wind.Degree)
fmt.Println("Country is: ", data.Sys.Country)
fmt.Println("City is: ", data.Name)
///////////////// CAN'T ACCESS Description...or anything in Weather
// fmt.Println("Country is: ", data.Weather.Descrip) // cannot access due to this portion being inside an array
}
/////////////////THIS IS THE JSON DATA THAT IS AVAILABLE ///////////
{
"coord": {
"lon": -97.31,
"lat": 32.94
},
"weather": [ // CAN'T ACCESS THIS CORRECTLY
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 306.46,
"pressure": 1014,
"humidity": 55,
"temp_min": 306.15,
"temp_max": 307.15
},
"visibility": 16093,
"wind": {
"speed": 5.1,
"deg": 150,
"gust": 7.2
},
"clouds": {
"all": 1
},
"dt": 1499120100,
"sys": {
"type": 1,
"id": 2597,
"message": 0.0225,
"country": "US",
"sunrise": 1499081152,
"sunset": 1499132486
},
"id": 0,
"name": "Fort Worth",
"cod": 200
}
You have to define slice of Weather struct in WeatherData.
Uncomment Weather struct and update WeatherData struct to following.
// WeatherData struct to collect data from the API call
type WeatherData struct {
Wind Wind `json:"wind"`
Sys Sys `json:"sys"`
Weather []Weather `json:"weather"`
Name string `json:"name"`
}
Please have a look on example code: https://play.golang.org/p/4KFqRuxcx2