Golang : best way to unmarshal following json with string as keys - json

I have json like
{
"api_type" : "abc",
"api_name" : "xyz",
"cities" : {
"new_york" : {
"lat":"40.730610",
"long":"-73.935242"
},
"london" : {
"lat":"51.508530",
"long":"-0.076132"
},
"amsterdam" : {
"lat":"52.379189",
"long":"4.899431"
}
//cities can be multiple
}
}
I can use following struct to unmarshal
type MyJsonName struct {
APIName string `json:"api_name"`
APIType string `json:"api_type"`
Locations struct {
Amsterdam struct {
Lat string `json:"lat"`
Long string `json:"long"`
} `json:"amsterdam"`
London struct {
Lat string `json:"lat"`
Long string `json:"long"`
} `json:"london"`
NewYork struct {
Lat string `json:"lat"`
Long string `json:"long"`
} `json:"new_york"`
} `json:"locations"`
}
but my city names and numbers will be different in each response, what is a best way unmarshal this type of json where keys can be string which varies.

I would make the locations a map (although you have it called cities in the JSON):
type MyJsonName struct {
APIName string `json:"api_name"`
APIType string `json:"api_type"`
Locations map[string]struct {
Lat string
Long string
} `json:"locations"`
}

Related

Parsing JSON response from Google Maps

I needed to create a proxy-server to connect to the google maps webservicees, and I found a tutorial that does about 95% of what I need. The tutorial uses golang, I'm almost completely new to golang, and if I follow it exactly, it works fine. But the moment I try to change something from the tutorial, I'm obviously messing something up, lol.
The issue that I need right now is that the tutorial only parses 2 variables from the google-maps response, the latitude and longitude. For the rest of my app I ALSO need the place Id.
I'm receiving the response from google-maps, not a problem. If I parse it as
type placeResults struct {
Results []struct {
Geometry struct {
Location struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"location"`
} `json:"geometry"`
} `json:"results"`
}
It works fine and gives me the latitude and longitude, no worries.
But if I instead try,
type placeResults struct {
Results []struct {
Geometry struct {
Location struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"location"`
} `json:"geometry"`
id struct {
Id string `json:"id"`
}
} `json:"results"`
}
It tells me:
json: cannot unmarshal string into Go struct field .Id of type struct { Id string "json:\"id\"" }
Now the id variable isn't part of the geometry, but is a generic part of the response, so I figured this would be the correct syntax. Obviously I am wrong. But what IS the correct syntax to include this?
Google place API response looks like,
"geometry" : {
"location" : {
"lat" : -33.866651,
"lng" : 151.195827
},
"viewport" : {
"northeast" : {
"lat" : -33.8653881697085,
"lng" : 151.1969739802915
},
"southwest" : {
"lat" : -33.86808613029149,
"lng" : 151.1942760197085
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "4f89212bf76dde31f092cfc14d7506555d85b5c7",
So you need to declare id field as string, not struct. You need to change placeResult structure.
type modifiedPlaceResult struct {
Geometry struct {
Location struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"location"`
} `json:"geometry"`
Id string `json:"id"`
}
try use modifiedPlaceResult struct for Unmarshaling data, everything would be ok.
I attached the example from Go playground: https://play.golang.org/p/Q4rM-jluoFe

GoLang JSON payload preparation with multiple data

I want to create JSON payload in given below format. I want a code or pattern that prepares the given format.
{
transactiontype: 'DDDDD'
emailType: 'QQQQQQ'
template: {
templateUrl: 'xry.kk'
templateName: 'chanda'
}
date: [
{
UserId: 1
Name: chadnan
},
{
UserId: 2
Name: kkkkkk
}
]
}
Hope this helps :
type Template struct {
TemplateURL string `json:"templateUrl" param:"templateUrl"`
TemplateName string `json:"templateName" param:"templateName"`
}
type Date struct {
UserId string `json:"UserId" param:"UserId"`
Name string `json:"Name" param:"Name"`
}
type NameAny struct {
*Template
TransactionType string `json:"transactiontype" param:"transactiontype"`
EmailType string `json:"emailType" param:"emailType"`
Data []Date `json:"date" param:"date"`
}
Data, _ := json.Marshal(NameAny)
Json(c, string(Data))(w, r)
You can use an online tool to convert a json into a valid Go struct: https://mholt.github.io/json-to-go/
Given your JSON, the Go struct is:
type AutoGenerated struct {
Transactiontype string `json:"transactiontype"`
EmailType string `json:"emailType"`
Template struct {
TemplateURL string `json:"templateUrl"`
TemplateName string `json:"templateName"`
} `json:"template"`
Date []struct {
UserID int `json:"UserId"`
Name string `json:"Name"`
} `json:"date"`
}
After the conversion, use the json.Marshal (Go Struct to JSON) and json.Unmarshal (JSON to Go Struct)
Complete example with your data: https://play.golang.org/p/RJuGK4cY1u-
// Transaction is a struct which stores the transaction details
type Transaction struct {
TransactionType string `json:"transaction_type"`
EmailType string `json:"email_type"`
Template Template `json:"template"`
Date []Date `json:"date"`
}
//Template is a struct which stores the template details
type Template struct {
TemplateURL string `json:"template_url"`
TemplateName string `json:"template_name"`
}
// Date is a struct which stores the user details
type Date struct {
UserID int `json:"user_id"`
Name string `json:"name"`
}
Above given structs are the correct data structure for storing your json body you can use json decoder for perfectly storing the data into struct
func exampleHandler(w http.ResponseWriter, r *http.Request) {
var trans Transaction
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&trans)
if err != nil {
log.Println(err)
}
}

How to simplify marshaling of deeply-nested JSON

I have a JSON which contain some static and dynamic data. Below is a sample of JSON
{
"request": { /*Static data start */
"data": {
"object": { /*Static data ends here*/
"user": { /*Dynamic data start here */
"userid": "andmmdn",
"ipaddr": "1.1.1.1",
"noofusers": "100",
"qos": "34",
"id": "kldjflkdfjlkdjfkld",
"domain": "xyz.com" /*Dynamic data ends here */
}
}
}
}
}
Below is the code which can create this JSON
package main
import (
"fmt"
"encoding/json"
)
//ReqJSON struct
type ReqJSON struct {
Request Request `json:"request"`
}
//Request struct
type Request struct {
Data Data `json:"data"`
}
//Data struct
type Data struct {
Object Object `json:"object"`
}
//Object struct
type Object struct {
User User `json:"user"`
}
//User struct
type User struct {
UserID string `json:"userid"`
IPAddr string `json:"ipaddr"`
Noofusers string `json:"noofusers"`
Qos string `json:"qos"`
ID string `json:"id"`
Domain string `json:"domain"`
}
func main() {
test := ReqJSON {
Request{
Data: Data{
Object: Object{
User: User{
UserID: "andmmdn",
IPAddr: "1.1.1.1",
Noofusers: "100",
Qos: "34",
ID: "kldjflkdfjlkdjfkld",
Domain: "xyz.com",
},
},
},
},
}
jsonEncode, _ := json.Marshal(test)
jsonIdent, _ := json.MarshalIndent(&test, "", "\t")
fmt.Println(string(jsonEncode))
fmt.Println(string(jsonIdent))
}
As you can see from above it contain struct which doesn't make much sense as they act more like placeholder for nesting the data. So how we make it more optimized. As all the data is being taken care in the last struct. What approach for unmarshaling of the data should be applied as the response will be in same format and want to use the last struct for the same.
Any thoughts on the approach.
Also how to make a generic struct as I multiple API which uses same struct below is an example
//ReqJSON for populating data
type ReqJSON struct {
Request struct {
Data struct {
Object struct {
Auth Auth `json:"auth"`
} `json:"object"`
} `json:"data"`
} `json:"request"`
}
//ReqJSON for populating data
type ReqJSON struct {
Request struct {
Data struct {
Object struct {
Error Error `json:"error"`
} `json:"object"`
} `json:"data"`
} `json:"request"`
}
If you don't need the wrapping types for anything besides marshaling/unmarshaling, you can define them anonymously:
type ReqJSON struct {
Request struct {
Data struct {
Object struct {
User User `json:"user"`
} `json:"object"`
} `json:"data"`
} `json:"request"`
}
type User struct {
UserID string `json:"userid"`
IPAddr string `json:"ipaddr"`
Noofusers string `json:"noofusers"`
Qos string `json:"qos"`
ID string `json:"id"`
Domain string `json:"domain"`
}
And, borowing from icza's answer, you can add accessor methods to ReqJSON:
func (j *ReqJSON) User() User { return j.Request.Data.Object.User }
func (j *ReqJSON) SetUser(u User) { j.Request.Data.Object.User = u }
func main() {
var j ReqJSON
j.SetUser(User{
UserID: "_id",
IPAddr: "1.1.1.1",
Noofusers: "100",
Qos: "34",
ID: "kldjflkdfjlkdjfkld",
Domain: "xyz.com",
})
b, err := json.MarshalIndent(j, "", " ")
fmt.Println(err, string(b))
}
That sounds about right. The solution is a little verbose / redundant, but so is the data format you have to deal with.
To work with that easily, you may create helper functions and use them:
func wrap(u User) *ReqJSON {
return &ReqJSON{Request: Request{Data: Data{Object: Object{User: u}}}}
}
func unwrap(r *ReqJSON) User {
return r.Request.Data.Object.User
}
But other than that, you can't really simplify other things.
So marshaling a User is like:
var u User
data, err := json.Marshal(wrap(u))
Unmarshaling is:
var r *ReqJSON
err := json.Unmarshal(data, &r)
// Check error
u := unwrap(r) // Here we have the user
You can't eliminate the complexity, but you could potentially hide some of it inside of a custom Marshaler:
type Request struct {
UserID string `json:"userid"`
IPAddr string `json:"ipaddr"`
Noofusers string `json:"noofusers"`
Qos string `json:"qos"`
ID string `json:"id"`
Domain string `json:"domain"`
}
func (r *Request) MarshalJSON() ([]byte, error) {
type request struct {
Data struct {
Object struct {
User struct {
Request
} `json:"user"`
} `json:"object"`
} `json:"data"`
}
structure := request{Data: data{Object: object{User: user{r}}}}
return json.Marshal(structure)
}
The same approach can be employed in reverse for UnmarshalJSON, if desired.

Unmarshall JSON with a [int]string map contained in an array

Im trying to unmarshall the following JSON into a struct, but there I am unable to translate the contents of the values field with the [[int,string]]
This is what I have so far:
type Response struct {
Metric struct {
Name string `json:"name,omitempty"`
Appname string `json:"appname,omitempty"`
} `json:"metric,omitempty"`
Values []map[int]string `json:"values,omitempty"`
}
The JSON file:
{
"metric":{
"name":"x444",
"appname":"cc-14-471s6"
},
"values":[
[
1508315264,
"0.0012116165566900816"
],
[
1508315274,
"0.0011871631158857396"
]
]
}
The data you showed should be unmarshaled to:
type Response struct {
Metric struct {
Name string `json:"name,omitempty"`
Appname string `json:"appname,omitempty"`
} `json:"metric,omitempty"`
Values [][]interface{} `json:"values,omitempty"`
}
If you want to to transfer it to map implement json.Unmarshaller interface - https://golang.org/pkg/encoding/json/#Unmarshaler
You can have something like:
type Item struct {
Key int
Val string
}
func(item *Item) UnmarshalJSON([]byte) error {
// TODO: implement
}
type Response struct {
Metric struct {
Name string `json:"name,omitempty"`
Appname string `json:"appname,omitempty"`
} `json:"metric,omitempty"`
Values []Item `json:"values,omitempty"`
}

What will be the structure representation of the following json data in golang?

{
"devices": [
{
"id": 20081691,
"targetIp": "10.1.1.1",
"iops": "0.25 IOPS per GB",
"capacity": 20,
"allowedVirtualGuests": [
{
"Name": "akhil1"
},
{
"Name": "akhil2"
}
]
}
]
}
How to write a structure representation of this JSON data so that I can add and delete devices to the list. I tried with the different structure representations but nothing is working. Below is one of the example I have tried with a similar kind of json data. I am not able to add new data to it. The structure representation and the way the append is done might be wrong here
package main
import (
"encoding/json"
"fmt"
)
type Person struct {
ID string `json:"id,omitempty"`
Firstname string `json:"firstname,omitempty"`
Lastname string `json:"lastname,omitempty"`
Address []Address `json:"address,omitempty"`
}
type Address[] struct {
City string `json:"city,omitempty"`
}
func main() {
var people []Person
people = append(people, Person{ID: "1", Firstname: "Nic", Lastname: "Raboy", Address: []Address{{City: "Dublin"},{ City: "CA"}}} )
b, err := json.Marshal(people)
if err != nil {
fmt.Println("json err:", err)
}
fmt.Println(string(b))
}
It will be below. This was generated using the excellent JSON-to-GO tool:
type MyStruct struct {
Devices []struct {
ID int `json:"id"`
TargetIP string `json:"targetIp"`
Iops string `json:"iops"`
Capacity int `json:"capacity"`
AllowedVirtualGuests []struct {
Name string `json:"Name"`
} `json:"allowedVirtualGuests"`
} `json:"devices"`
}
To simplify that though, you can break it into smaller structs for readability. An example is below:
package main
import "fmt"
type VirtualGuest struct {
Name string `json:"Name"`
}
type Device struct {
ID int `json:"id"`
TargetIP string `json:"targetIp"`
Iops string `json:"iops"`
Capacity int `json:"capacity"`
AllowedVirtualGuests []VirtualGuest `json:"allowedVirtualGuests"`
}
type MyStruct struct {
Devices []Device `json:"devices"`
}
func main() {
var myStruct MyStruct
// Add a MyStruct
myStruct.Devices = append(myStruct.Devices, Device{
ID:1,
TargetIP:"1.2.3.4",
Iops:"iops",
Capacity:1,
AllowedVirtualGuests:[]VirtualGuest{
VirtualGuest{
Name:"guest 1",
},
VirtualGuest{
Name:"guest 2",
},
},
})
fmt.Printf("MyStruct: %v\n", myStruct)
}
you can use struct tag like json:"id", try struct below:
type Data struct {
Devices []struct {
Id int `json:"id"`
IP string `json:"targetIp"`
IOPS string `json:"iops"`
Capacity int `json:"capacity"`
AllowedVirtualGuests []struct {
Name string `json:"Name"`
} `json:"allowedVirtualGuests"`
} `json:"devices"`
}