Iterating list json object in golang - go

I have this piece of code to read a JSON object. I need to easily iterate over all the elements in the 'outputs'/data/concepts key.
Is there a better way to do it?
Also, how can I access the attributes of value:
value.app_id, value.id..etc
Code:
package main
import (
"encoding/json"
"fmt"
)
var jsonBytes = []byte(`
{"outputs": [{
"data": {"concepts":
[{"app_id": "main",
"id": "ai_GTvMbVGh",
"name": "ancient",
"value": 0.99875855}]
}}
],
"status": {"code": 10000, "description": "Ok"}}`)
func main() {
var output map[string]interface{}
err := json.Unmarshal([]byte(jsonBytes), &output)
if err != nil {
print(err)
}
for _, value := range output["outputs"].([]interface{}) {
//fmt.Println(value.(map[string]interface{})["data"].(map[string]interface{})["concepts"]).([]interface{})
//fmt.Println(value.(map[string]interface{})["data"].(map[string]interface{})["concepts"])
for _, value := range value.(map[string]interface{})["data"].(map[string]interface{})["concepts"].([]interface{}){
fmt.Println(value)
}
}
//fmt.Printf("%+v\n", output)
}

the best way will be to Unmarshal the JSON into an struct and iterate over the values,
func main() {
var output StructName
err := json.Unmarshal([]byte(jsonBytes), &output)
if err != nil {
print(err)
}
for _, value := range output.Outputs {
for _, val := range value.Data.Concepts {
fmt.Printf("AppId:%s\nID:%s\nname:%s\nvalue:%f", val.AppID, val.ID, val.Name, val.Value)
}
}
}
type StructName struct {
Outputs []struct {
Data struct {
Concepts []struct {
AppID string `json:"app_id"`
ID string `json:"id"`
Name string `json:"name"`
Value float64 `json:"value"`
} `json:"concepts"`
} `json:"data"`
} `json:"outputs"`
Status struct {
Code int `json:"code"`
Description string `json:"description"`
} `json:"status"`
}

Related

Convert JSON key value pair into single field value in Go

I have a json like following, where value can be int or string
{
"data": [
{
"Name": "a_name",
"value": 1
},
{
"Name": "b_name",
"value": "val"
},
{
"Name": "c_name",
"value": 2
}
]
}
Now I want to convert that json into following struct, like only extract a_name and b_name value.
type Data struct {
AName int `json: "a_name"`
BName string `json: "b_name"`
}
I can do it by following way
import (
"encoding/json"
"fmt"
)
type TmpData struct {
Data []struct {
Name string `json:"Name"`
Value interface{} `json:"value"`
} `json:"data"`
}
type ExpectedData struct {
AName int `json: "a_name"`
BName string `json: "b_name"`
}
func main() {
data := `{
"data": [
{
"Name": "a_name",
"value": 1
},
{
"Name": "b_name",
"value": "val"
},
{
"Name": "c_name",
"value": 2
}
]
}`
tmpData := &TmpData{}
json.Unmarshal([]byte(data), tmpData)
ans := &ExpectedData{}
for _, d := range tmpData.Data {
if d.Name == "a_name" {
ans.AName = int(d.Value.(float64))
} else if d.Name == "b_name" {
ans.BName = d.Value.(string)
}
}
fmt.Println(ans)
}
Is there any better solution for this?
Not possible with the standard JSON un-marshalling unless you write a custom un-marshaller for your Data type.
The key here is to define the type for value to be an interface{}, so that multiple types could be stored in your b_name record.
func (d *Data) UnmarshalJSON(data []byte) error {
var result Details
if err := json.Unmarshal(data, &result); err != nil {
return err
}
for _, value := range result.Data {
switch value.Name {
// The json package will assume float64 when Unmarshalling with an interface{}
case "a_name":
v, ok := (value.Value).(float64)
if !ok {
return fmt.Errorf("a_name got data of type %T but wanted float64", value.Value)
}
d.AName = int(v)
case "b_name":
v, ok := (value.Value).(string)
if !ok {
return fmt.Errorf("b_name got data of type %T but wanted string", value.Value)
}
d.BName = v
}
}
return nil
}
Playground - https://go.dev/play/p/GrXKAE87d1F

Parsing Nested JSON string

I am trying to parse a nested json string
I did get it to work by using multiple structs, but I am wondering if I can parse the JSON without using an extra struct.
type Events struct {
Events []Event `json:"events"`
}
type Event struct {
Name string `json:"name"`
Url string `json:"url"`
Dates struct {
Start struct {
LocalDate string
LocalTime string
}
}
}
type Embed struct {
TM Events `json:"_embedded"`
}
func TMGetEventsByCategory(location string, category string) {
parameters := "city=" + location + "&classificationName=" + category + "&apikey=" + api_key
tmUrl := tmBaseUrl + parameters
resp, err := http.Get(tmUrl)
var embed Embed
var tm Event
if err != nil {
log.Printf("The HTTP request failed with error %s\n", err)
} else {
data, _ := ioutil.ReadAll(resp.Body)
err := json.Unmarshal(data, &embed)
json.Unmarshal(data, &tm)
}
}
JSON Data looks like this:
{
"_embedded": {
"events": [],
},
"OtherStuff": {
}
}
Is it possible to get rid of the Embed struct and read straight to the events part of the json string?
What you're looking for here is json.RawMessage. It can help delay parsing of certain values, and in you case map[string]json.RawMessage should represent the top-level object where you can selectively parse values. Here's a simplified example you can adjust to your case:
package main
import (
"encoding/json"
"fmt"
)
type Event struct {
Name string `json:"name"`
Url string `json:"url"`
}
func main() {
bb := []byte(`
{
"event": {"name": "joe", "url": "event://101"},
"otherstuff": 15.2,
"anotherstuff": 100
}`)
var m map[string]json.RawMessage
if err := json.Unmarshal(bb, &m); err != nil {
panic(err)
}
if eventRaw, ok := m["event"]; ok {
var event Event
if err := json.Unmarshal(eventRaw, &event); err != nil {
panic(err)
}
fmt.Println("Parsed Event:", event)
} else {
fmt.Println("Can't find 'event' key in JSON")
}
}
In your case look for the _embedded key and then Unmarshal its value to Events
yes of course
type Embed struct {
TM []struct {
Name string `json:"name"`
Url string `json:"url"`
Dates struct {
Start struct {
LocalDate string
LocalTime string
}
} // add tag here if you want
} `json:"_embedded"`
}

Marshall/Unmarshal JSONPB

I am trying to Unmarshal some json data to a proto message.
JSON
{
"id": 1,
"first_name": "name",
"phone_numbers": []
}
Proto
message Item {
uint32 id=1;
string name=2;
repeated string numbers=3;
}
Proto.go
type Item struct {
Id uint32 `protobuf:"varint,1,opt,name=id" json:"id,omitempty"`
Name string `protobuf:"bytes,2,opt,name=name" json:"name,omitempty"`
Numbers []string `protobuf:"bytes,4,rep,name=numbers" json:"numbers,omitempty"`
}
How can I map the above JSON to my proto Message (from what I can see there is no way to specify tags in proto atm)?
Your JSON document doesn't match the proto definition; name != first_name and numbers != phone_numbers.
You can define another type that has the same fields as Item but different struct tags and then convert to Item:
var x struct {
Id uint32 `json:"id,omitempty"`
Name string `json:"first_name,omitempty"`
Numbers []string `json:"phone_numbers,omitempty"`
}
if err := json.Unmarshal(jsonDoc, &x); err != nil {
log.Fatal(err)
}
var i = Item(x)
If every JSON document you want to decode has this structure, it may be more convenient to let Item implement json.Unmarshaler:
package main
import (
"encoding/json"
"fmt"
"log"
)
var jsonDoc = []byte(`
{
"id": 1,
"first_name": "name",
"phone_numbers": [
"555"
]
}
`)
type Item struct {
Id uint32 `protobuf:"varint,1,opt,name=id" json:"id,omitempty"`
Name string `protobuf:"bytes,2,opt,name=name" json:"name,omitempty"`
Numbers []string `protobuf:"bytes,4,rep,name=numbers" json:"numbers,omitempty"`
}
// You can define this function is item_json.go or so, then it
// isn't removed if you re-generate your types.
func (i *Item) UnmarshalJSON(b []byte) error {
type item struct {
Id uint32 `json:"id,omitempty"`
Name string `json:"first_name,omitempty"`
Numbers []string `json:"phone_numbers,omitempty"`
}
var x item
if err := json.Unmarshal(jsonDoc, &x); err != nil {
return err
}
*i = Item(x)
return nil
}
func main() {
var i Item
if err := json.Unmarshal(jsonDoc, &i); err != nil {
log.Fatal(err)
}
fmt.Printf("%#v\n", i)
}
Try it on the playground: https://play.golang.org/p/0qibavRJbwi

Unmarshalling array into struct

I'm trying to figure out how I can (using gin) create a struct from an api call
"icon": [
"https://api.figo.me/assets/images/accounts/postbank.png",
{
"48x48": "https://api.figo.me/assets/images/accounts/postbank_48.png",
"60x60": "https://api.figo.me/assets/images/accounts/postbank_60.png",
"72x72": "https://api.figo.me/assets/images/accounts/postbank_72.png",
"84x84": "https://api.figo.me/assets/images/accounts/postbank_84.png",
"96x96": "https://api.figo.me/assets/images/accounts/postbank_96.png",
"120x120": "https://api.figo.me/assets/images/accounts/postbank_120.png",
"144x144": "https://api.figo.me/assets/images/accounts/postbank_144.png",
"192x192": "https://api.figo.me/assets/images/accounts/postbank_192.png",
"256x256": "https://api.figo.me/assets/images/accounts/postbank_256.png"
}
],
into
type CatalogBank struct {
Advice string `json:"advice"`
BankCode string `json:"bank_code"`
BankName string `json:"bank_name"`
BIC string `json:"bic"`
Credentials []struct {
Label string `json:"label"`
Masked bool `json:"masked"`
} `json:"credentials"`
Icon []struct {
} `json:"icon"`
Language []byte `json:"language"`
}
The icon part is just an extract from, but I always get an unmarshall error for this part. How would I have to definde the 'Icon' part in the struct?
This would work
package main
type CatalogBank struct {
Icon []interface{} `json:"icon"`
}
This is a little tricky in Golang because of the non-strict type in the JSON. If that is definitely the format you are going to receive the data in, you should unmarshal to an Interface{} and then parse the interface into a struct that you can use in your Golang
Direct Unmarshalling cannot be done, as the type of each field is not known
type Icon struct{
ImageLink string
ImageLink48 string
// ...
}
type CatalogBank struct {
Advice string `json:"advice"`
IconRaw []interface{} `json:"icon"`
Icon []Icon
//...
}
func UnmarshalIcon(c &CatalogBank, i interface{}):
// first convert it to the top level list
newIcon := Icon{}
listOfIcons := i.([]interface{})
for _, i := range listOfIcons:
switch iT := i.(type) {
case string:
newIcon.ImageLink = iT
case map[string]interface{}:
for smallIconsKey, smallIconLink := range iT {
if smallIconsKey == "48x48"{
newIcon.ImageLink48 = smallIconLink.(string)
}
// and so on
}
var c CatalogBank{}
_ := json.Unmarshal([]byte(your_json), &c)
for _, i := range c.IconRaw:
UnmarshalIcon(&c, i)
Caveat Emptor: I haven't checked above implementation but it should be something like this
You can not use []struct {} for icon, change it to []interface{} instead, or if you want operate on type safe type look at the second solution with cusom unmarshaler
Solution 1
package main
import (
"encoding/json"
"fmt"
"log"
)
type CatalogBank struct {
Advice string `json:"advice"`
BankCode string `json:"bank_code"`
BankName string `json:"bank_name"`
BIC string `json:"bic"`
Credentials []struct {
Label string `json:"label"`
Masked bool `json:"masked"`
} `json:"credentials"`
Icon []interface{} `json:"icon"`
Language []byte `json:"language"`
}
func main() {
data := `
{
"Advice":"abc",
"icon": [
"https://api.figo.me/assets/images/accounts/postbank.png",
{
"48x48": "https://api.figo.me/assets/images/accounts/postbank_48.png",
"60x60": "https://api.figo.me/assets/images/accounts/postbank_60.png",
"72x72": "https://api.figo.me/assets/images/accounts/postbank_72.png",
"84x84": "https://api.figo.me/assets/images/accounts/postbank_84.png",
"96x96": "https://api.figo.me/assets/images/accounts/postbank_96.png",
"120x120": "https://api.figo.me/assets/images/accounts/postbank_120.png",
"144x144": "https://api.figo.me/assets/images/accounts/postbank_144.png",
"192x192": "https://api.figo.me/assets/images/accounts/postbank_192.png",
"256x256": "https://api.figo.me/assets/images/accounts/postbank_256.png"
}
]
}
`
bank := &CatalogBank{}
err := json.Unmarshal([]byte(data), bank)
if err != nil {
log.Fatal(err)
}
for _, icon := range bank.Icon {
fmt.Printf(" %v\n ", icon)
}
}
Solution 2:
package main
import (
"encoding/json"
"fmt"
"log"
)
type Icons struct {
URL string
BySize map[string]string
}
type CatalogBank struct {
Advice string `json:"advice"`
BankCode string `json:"bank_code"`
BankName string `json:"bank_name"`
BIC string `json:"bic"`
Credentials []struct {
Label string `json:"label"`
Masked bool `json:"masked"`
} `json:"credentials"`
Icon *Icons `json:"-,"`
Language []byte `json:"language"`
}
func (p *CatalogBank) Unmarshal(data []byte) error {
type Transient struct {
*CatalogBank
Icon []interface{} `json:"icon"`
}
var transient = &Transient{CatalogBank:p}
err := json.Unmarshal([]byte(data), transient)
if err != nil {
return err
}
p.Icon = &Icons{
BySize: make(map[string]string),
}
if len(transient.Icon) > 0 {
if url, ok := transient.Icon[0].(string); ok {
p.Icon.URL = url
}
if aMap, ok := transient.Icon[1].(map[string]interface{}); ok {
for k, v := range aMap {
p.Icon.BySize[k] = v.(string)
}
}
}
return nil
}
func main() {
data := `
{
"Advice":"abc",
"icon": [
"https://api.figo.me/assets/images/accounts/postbank.png",
{
"48x48": "https://api.figo.me/assets/images/accounts/postbank_48.png",
"60x60": "https://api.figo.me/assets/images/accounts/postbank_60.png",
"72x72": "https://api.figo.me/assets/images/accounts/postbank_72.png",
"84x84": "https://api.figo.me/assets/images/accounts/postbank_84.png",
"96x96": "https://api.figo.me/assets/images/accounts/postbank_96.png",
"120x120": "https://api.figo.me/assets/images/accounts/postbank_120.png",
"144x144": "https://api.figo.me/assets/images/accounts/postbank_144.png",
"192x192": "https://api.figo.me/assets/images/accounts/postbank_192.png",
"256x256": "https://api.figo.me/assets/images/accounts/postbank_256.png"
}
]
}
`
bank := &CatalogBank{}
err := bank.Unmarshal([]byte(data))
if err != nil {
log.Fatal(err)
}
fmt.Printf("advice: %v\n", bank.Advice)
fmt.Printf("icon: %v\n", bank.Icon.URL)
for size, icon := range bank.Icon.BySize {
fmt.Printf("%v => %v\n ",size, icon)
}
}
You can define your icon like this:
package main
import (
"fmt"
"encoding/json"
)
var testIcon = []byte(`{"icon":[
"https://api.figo.me/assets/images/accounts/postbank.png",
{
"48x48":"https://api.figo.me/assets/images/accounts/postbank_48.png"
}]
}`)
func main() {
icon := make(map[string][]interface{})
err := json.Unmarshal(testIcon, &icon)
if err != nil {
panic(err)
}
fmt.Println(icon)
// map[icon:[https://api.figo.me/assets/images/accounts/postbank.png map[48x48:https://api.figo.me/assets/images/accounts/postbank_48.png]]]
}

Convert Struct to JSON in Golang

New to Golang here and I'm trying to get a struct to convert to a JSON object that one of my other applications will consume.
The expected response will look something like this...
"access": {
"STOCK": "1",
"FOREX": "1",
"WEBFOREX": "1",
"WEBSTOCK": "1"
},
"subscription_group_dates": {
"32": {
"START_DATE": 1464753600,
"END_DATE": 1472616000
},
"42": {
"START_DATE": 1470024000,
"END_DATE": 1472616000
}
}
I have left in the "access" part of the object to show the parts I know work. Everything else has been omitted for brevity. The tough part for me is handling the nesting nature of structs in golang... The following Struct I think will work, but I'm not sure about the syntax.
Under the SubscriptionGroupDates Type... can I use map[string]struct and embed the rest as shown?. see below
type UCSUserAccess struct {
Groups map[string]string `json:"groups"`
Access map[string]string `json:"access"`
IsExpert string `json:"isExpert"`
SubscriptionGroupDates map[string]struct {
GroupID struct {
StartDate map[string]int `START_DATE`
EndDate map[string]int `END_DATE`
}
} `json:"subscription_group_dates"`
}
In addition... I have imported the "time" library in the import section. How would I use that to declare the dates as time objects? (instead of map[string]int)
Thanks
Your structure is a bit off. SubscriptionGroupDates, specifically, is a map of string to a structure, and the structure itself is either a map of strings to ints, or a static structure with two int fields. You have a second struct nested, and your dates are specified as maps when they should just be ints:
type UCSUserAccess struct {
Groups map[string]string `json:"groups"`
Access map[string]string `json:"access"`
IsExpert string `json:"isExpert"`
SubscriptionGroupDates map[string]GroupID `json:"subscription_group_dates"`
}
type GroupID struct {
StartDate int `json:"START_DATE"`
EndDate int `json:"END_DATE"`
}
This gives the JSON as you expect. Example: https://play.golang.org/p/rGm7zKJypk (run the output through jsonlint.com to verify it comes out how you wanted).
As for the timestamp, your timestamps in the JSON are stored as Unix timestamps. In order to get those into a time.Time, you need to parse them yourself, either after unmarshalling, or by implementing the json.Unmarshaller interface on a custom time type. See this answer for details on how to do that.
You could create your own date simple by supplying UnmarshalJSON and MarshalJSON methods. You can format the handle the input and format the output in any way you please.
package main
import (
"encoding/json"
"fmt"
"strconv"
"strings"
)
type Date struct {
year int
month int
day int
}
func (t *Date) UnmarshalJSON(data []byte) (e error) {
trimmed := string(data)
trimmed = strings.TrimLeft(trimmed, "\"")
trimmed = strings.TrimRight(trimmed, "\"")
parts := strings.Split(trimmed, "-")
t.year, _ = strconv.Atoi(parts[0])
t.month, _ = strconv.Atoi(parts[1])
t.day, _ = strconv.Atoi(parts[2])
return
}
func (t *Date) MarshalJSON() (buff []byte, e error) {
buff = []byte(fmt.Sprintf("\"%d-%d-%d\"", t.year, t.month, t.day))
return
}
type Foo struct {
Groups map[string]string `json:"groups"`
Date Date `json:"date"`
}
func main() {
f := Foo{
Groups: map[string]string{
"group1": "bar",
"group2": "baz",
},
Date: Date{year: 2016, month: 12, day: 22},
}
buff, _ := json.Marshal(&f)
fmt.Println(string(buff))
json.Unmarshal(buff, &f)
fmt.Printf("%+v", f)
}
1- You may use Marshal and Unmarshal time, like this working sample code:
package main
import (
"encoding/json"
"fmt"
"log"
"strconv"
"time"
)
type UCSUserAccess struct {
Groups map[string]string `json:"groups"`
Access map[string]string `json:"access"`
IsExpert string `json:"isExpert"`
SubscriptionGroupDates map[string]struct {
StartDate Time `json:"START_DATE"`
EndDate Time `json:"END_DATE"`
} `json:"subscription_group_dates"`
}
type Time time.Time
func (t Time) MarshalJSON() ([]byte, error) {
data := []byte(fmt.Sprint(time.Time(t).UTC().Unix()))
return data, nil
}
func (t *Time) UnmarshalJSON(data []byte) error {
i, e := strconv.ParseInt(string(data), 10, 64)
*t = Time(time.Unix(i, 0).UTC())
return e
}
func (t Time) String() string {
return time.Time(t).UTC().String()
}
func main() {
str := `{
"access": {
"STOCK": "1",
"FOREX": "1",
"WEBFOREX": "1",
"WEBSTOCK": "1"
},
"subscription_group_dates": {
"32": {
"START_DATE": 1464753600,
"END_DATE": 1472616000
},
"42": {
"START_DATE": 1470024000,
"END_DATE": 1472616000
}
}
}`
var d UCSUserAccess
err := json.Unmarshal([]byte(str), &d)
if err != nil {
log.Fatal(err)
}
fmt.Println(d)
fmt.Println()
body, err := json.Marshal(d)
if err != nil {
panic(err)
}
fmt.Println(string(body))
}
2- You may use Marshal and Unmarshal time, like this simplified working sample code:
package main
import (
"encoding/json"
"fmt"
"log"
"strconv"
"time"
)
type UCSUserAccess struct {
StartDate Time `json:"START_DATE"`
}
type Time time.Time
func (t Time) MarshalJSON() ([]byte, error) {
data := []byte(fmt.Sprint(time.Time(t).UTC().Unix()))
return data, nil
}
func (t *Time) UnmarshalJSON(data []byte) error {
i, e := strconv.ParseInt(string(data), 10, 64)
*t = Time(time.Unix(i, 0).UTC())
return e
}
func (t Time) String() string {
return time.Time(t).UTC().String()
}
func main() {
str := `{
"START_DATE": 1464753600
}`
var d UCSUserAccess
err := json.Unmarshal([]byte(str), &d)
if err != nil {
log.Fatal(err)
}
fmt.Println(d)
fmt.Println()
body, err := json.Marshal(d)
if err != nil {
panic(err)
}
fmt.Println(string(body))
}
3- Also you may use int64 for time,like this working sample code:
package main
import (
"encoding/json"
"fmt"
"log"
"time"
)
type GroupID struct {
StartDate int64 `json:"START_DATE"`
EndDate int64 `json:"END_DATE"`
}
func (t *GroupID) Start() time.Time {
return time.Unix(t.StartDate, 0)
}
func (t *GroupID) End() time.Time {
return time.Unix(t.EndDate, 0)
}
type UCSUserAccess struct {
Access map[string]string `json:"access"`
SubscriptionGroupDates map[string]GroupID `json:"subscription_group_dates"`
}
func main() {
str := `{
"access": {
"STOCK": "1",
"FOREX": "1",
"WEBFOREX": "1",
"WEBSTOCK": "1"
},
"subscription_group_dates": {
"32": {
"START_DATE": 1464753600,
"END_DATE": 1472616000
},
"42": {
"START_DATE": 1470024000,
"END_DATE": 1472616000
}
}
}`
var d UCSUserAccess
err := json.Unmarshal([]byte(str), &d)
if err != nil {
log.Fatal(err)
}
fmt.Println(d)
gID := d.SubscriptionGroupDates["32"]
fmt.Println(gID.Start())
fmt.Println()
body, err := json.Marshal(d)
if err != nil {
panic(err)
}
fmt.Println(string(body))
}
4- You may use Int64 with receiver method, like this working sample code:
package main
import (
"encoding/json"
"fmt"
"log"
"time"
)
type Int64 int64
type GroupID struct {
StartDate Int64 `json:"START_DATE"`
EndDate Int64 `json:"END_DATE"`
}
func (t *Int64) Time() time.Time {
return time.Unix(int64(*t), 0).UTC()
}
type UCSUserAccess struct {
Access map[string]string `json:"access"`
SubscriptionGroupDates map[string]GroupID `json:"subscription_group_dates"`
}
func main() {
str := `{
"access": {
"STOCK": "1",
"FOREX": "1",
"WEBFOREX": "1",
"WEBSTOCK": "1"
},
"subscription_group_dates": {
"32": {
"START_DATE": 1464753600,
"END_DATE": 1472616000
},
"42": {
"START_DATE": 1470024000,
"END_DATE": 1472616000
}
}
}`
var d UCSUserAccess
err := json.Unmarshal([]byte(str), &d)
if err != nil {
log.Fatal(err)
}
fmt.Println(d)
gID := d.SubscriptionGroupDates["32"]
fmt.Println(gID.StartDate.Time())
fmt.Println()
body, err := json.Marshal(d)
if err != nil {
panic(err)
}
fmt.Println(string(body))
}

Resources