Unable to decode int field from JSON - json

My goal is to read JSON file. Although the following program reads the file fine, the id field value is incorrect. id field value is 0 for all the objects read.
Example JSON file contents:
{
"users" : [
{
"id" : "1",
"name" : "Matt Hayden"
},
{
"id" : "2",
"name" : "David Warner"
},
]
}
JSON file reader program in Golang
type Schema struct {
Id int `json:"id"`
Name string `json:"name"`
}
func reader() {
fileName := "./input.json"
jsonFile, fileErr := os.Open(fileName)
if fileErr != nil {
_ = errors.Errorf("failed to open file %s : %v", fileName, fileErr)
}
defer jsonFile.Close()
bytesRead, readErr := ioutil.ReadAll(jsonFile)
if readErr != nil {
_ = errors.Errorf("failed to read json file %s : %v", fileName, readErr)
}
var sch Schema
unMarshalErr := json.Unmarshal(bytesRead, &sch)
if unMarshalErr != nil {
_ = errors.Errorf("failed to unmarshal JSON bytes : %v", unMarshalErr)
}
fmt.Printf("%+v", sch)
}
Actual output:
{Users:[{Id:0 Name:Matt Hayden} {Id:0 Name:David Warner}]}
Expected output:
{Users:[{Id:1 Name:Matt Hayden} {Id:2 Name:David Warner}]}

You can add an extra type in your JSON struct tag. Take a look at the Marshal documentation here.
Example: https://play.golang.org/p/VqZu9SrnpW4
package main
import (
"encoding/json"
"fmt"
"reflect"
)
type Response struct {
Users []User `json:"users"`
}
type User struct {
ID int `json:"id,string"`
Name string `json:"name"`
}
func main() {
r := &Response{}
err := json.Unmarshal([]byte(`{
"users" : [
{
"id" : "1",
"name" : "Matt Hayden"
},
{
"id" : "2",
"name" : "David Warner"
}
]
}`), &r)
if err != nil {
panic(err)
}
fmt.Printf("%#v \n", r)
fmt.Printf("Type of ID is %s \n", reflect.TypeOf(r.Users[0].ID)) // int
}

Related

Parse nested json array from a file

I have a json file sample.json containing a json array as follows:
[
{
"time": "2021-01-04T00:11:32.362Z",
"extra_data": {
"id": "123"
},
"info": "event123"
},
{
"time": "2021-01-05T00:11:32.362Z",
"extra_data": {
"id": "456"
},
"info": "event456"
},
{
"time": "2021-01-06T00:11:32.362Z",
"extra_data": {
"id": "789"
},
"info": "event789"
}
]
I am trying to unmarshal this json array so that for each json object (i.e. event), I can access the id and info values. This is what I have so far:
func main() {
file, err := ioutil.ReadFile("/Users/janedoe/Downloads/sample.json")
var events Event
json.Unmarshal([]byte(file), &events)
fmt.Println(reflect.TypeOf(events))
// Reading each value by its key for each event
fmt.Println("Event123_Time :", events.Timestamp,
"\nEvent123_ExtraData_Id :", events.ExtraData.Id,
"\nEvent123_Info :", events.Info)
}
type Event struct {
Time string `json:"time"`
ExtraData ExtraData `json:"extra_data"`
Info string `json:"info"`
}
type ExtraData struct {
Id string `json:"id"`
}
The output that I am getting is:
main.Event
Event123_Time :
Event123_ExtraData_Id :
Event123_Info :
I am not getting any values, implying that the marshalling is not happening as expected. How can I fix this?
Since your json is an array, I think you want:
var events []Event
not
var events Event
Here's a version with that change:
func main() {
file, err := ioutil.ReadFile("/Users/janedoe/Downloads/sample.json")
if err != nil {
panic(err)
}
var events []Event
if err := json.Unmarshal([]byte(file), &events); err != nil {
panic(err)
}
fmt.Println(reflect.TypeOf(events))
for i, event := range events {
// Reading each value by its key for each event
fmt.Println(i, "Event123_Time :", event.Time,
"\nEvent123_ExtraData_Id :", event.ExtraData.Id,
"\nEvent123_Info :", event.Info)
}
}
type Event struct {
Time string `json:"time"`
ExtraData ExtraData `json:"extra_data"`
Info string `json:"info"`
}
type ExtraData struct {
Id string `json:"id"`
}

Golang json unmarshal according to key value pair

I have json as following
"data": [
{
"id": "recent_search",
"items": [],
"name": ""
},
{
"id": "popular_search",
"items": [],
"name": ""
},
{
"id": "digital",
"items": [],
"name": "DIGITAL"
}
]
and the structs are as follows:
type universeTypeData struct {
Recent universeSearchInfo
Popular universeSearchInfo
Digital universeSearchInfo
}
type universeSearchInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Items []universeSearchItem `json:"items"`
}
I want to unmarshal my json as "id" with value "recent_search" map to Recent, "id" with value "popular_search" map to Popular. Is there any way of doing this in golang?
My approach of doing it is
for _, v := range result.Data {
if v.ID == "in_category" {
finalResult.Universe.InCategory.ID = v.ID
finalResult.Universe.InCategory.Name = v.Name
for _, abc := range v.Items {
finalResult.Universe.InCategory.Items = append(finalResult.Universe.InCategory.Items, abc)
}
}
if v.ID == "recent_search" {
finalResult.Universe.Recent.ID = v.ID
finalResult.Universe.Recent.Name = v.Name
for _, abc := range v.Items {
finalResult.Universe.Recent.Items = append(finalResult.Universe.Recent.Items, abc)
}
}
if v.ID == "popular_search" {
finalResult.Universe.Popular.ID = v.ID
finalResult.Universe.Popular.Name = v.Name
for _, abc := range v.Items {
finalResult.Universe.Popular.Items = append(finalResult.Universe.Popular.Items, abc)
}
}
Is there any better way of doing it?
You want to unmurshal JSON array into Go struct which is not natural mapping. Any way, you most likely should be first unmurshal in slice and then parse this slice. Some workaround is to use json.Decoder
dec := json.NewDecoder(JSONdataReader)
var res universeTypeData
// read open bracket
dec.Token()
// while the array contains values
for dec.More() {
var m universeSearchInfo
// decode an array value
dec.Decode(&m)
switch m.ID {
case "recent_search":
res.Recent = m
case "popular_search":
res.Popular = m
case "digital":
res.Digital = m
}
}
// read closing bracket
dec.Token()
which allow you to decode on the fly, in one pass, without consuming intermediate slice representation. Working example
Implement Unmarshaler interface:
Unmarshaler is the interface implemented by types that can unmarshal a
JSON description of themselves. The input can be assumed to be a valid
encoding of a JSON value. UnmarshalJSON must copy the JSON data if it
wishes to retain the data after returning.
json unmarshaler interface assign the value from json to struct after parsing the result and applying conditions to fetch the value.
package main
import (
"encoding/json"
"fmt"
)
type Details struct {
Data []universeSearchInfo `json:"data"`
}
type universeTypeData struct {
Recent universeSearchInfo
Popular universeSearchInfo
Digital universeSearchInfo
}
type universeSearchInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Items []string `json:"items"`
}
func main() {
var result universeTypeData
jsonBytes := []byte(`{"data": [
{
"id": "recent_search",
"items": [],
"name": ""
},
{
"id": "popular_search",
"items": [],
"name": ""
},
{
"id": "digital",
"items": [],
"name": "DIGITAL"
}
]}`)
if err := json.Unmarshal(jsonBytes, &result); err != nil {
fmt.Println(err)
}
fmt.Println(result)
}
func (universeData *universeTypeData) UnmarshalJSON(data []byte) error {
var result Details
if err := json.Unmarshal(data, &result); err != nil {
return err
}
for _,value := range result.Data{
switch value.ID {
case "recent_search":
universeData.Recent = value
}
}
return nil
}
Working code on Go Playground

Unmarshalling json arrays as json objects

I have to unmarshal a series of Json objects, but one of the objects contain a json array which is not really structured in a good way.
"labels": [
{
"key": "owner",
"value": "harry"
},
{
"key": "group",
"value": "student"
}
]
I am unmarshalling it using this struct -
type StudentDetails struct {
Id string `json:"id"`
Name string `json:"name"`
Labels []Label `json:"labels,omitempty"`
}
type Label struct {
Key string `json:"key"`
Value string `json:"value"`
}
And I have to access it using x.Labels[0].key == "owner" inside a for loop which is very annoying.
I want to be able to do x.Labels.Owner == "harry" instead. How do I go about achieving this? The rest of JSON is unmarshalled fine using the default unmarshal function, so I don't think writing custom function will be good option.
With the constraints you have here, this is about as close as you will get (run in playground):
package main
import (
"encoding/json"
"fmt"
)
func main() {
j := `
{
"id": "42",
"name": "Marvin",
"labels": [
{
"key": "owner",
"value": "harry"
},
{
"key": "group",
"value": "student"
}
]
}`
d := StudentDetails{}
err := json.Unmarshal([]byte(j), &d)
if err != nil {
panic(err)
}
fmt.Println(d.Labels["owner"])
fmt.Println(d.Labels["group"])
}
type StudentDetails struct {
Id string `json:"id"`
Name string `json:"name"`
Labels Labels `json:"labels"`
}
type Labels map[string]string
func (l *Labels) UnmarshalJSON(b []byte) error {
a := []map[string]string{}
err := json.Unmarshal(b, &a)
if err != nil {
return err
}
t := map[string]string{}
for _, m := range a {
t[m["key"]] = m["value"]
}
*l = t
return nil
}
How about to define custom []Label type and add function on it.
For instance
type Labels []Label
func (l Labels) Owner() string {
if len(l) > 1 {
return l[0].Value
}
return ""
}

Golang Unmarshal JSON

My input JSON, has a list of different elements.
I have problems with the number of the first element of response.
Simplified example:
package main
import (
"fmt"
"log"
"encoding/json"
)
var j = ` {
"response": [
777, // problem here !!!
{
"id": 888,
"from_id": 999,
"to_id": 888,
"text": "hello..."
},
{
"id": 999,
"from_id": 888,
"to_id": 999,
"text": "goodbye..."
}
]
}`
type D struct {
Id int `json:"id"`
FromId int `json:"from_id"`
ToId int `json:"to_id"`
Text string `json:"text"`
}
type R struct {
Count int
Response []D `json:"response"`
}
func main() {
var data = new(R)
err := json.Unmarshal([]byte(j), &data)
if err != nil {
log.Fatal(err)
}
fmt.Println(data.Response)
}
Error on output. I do not understand where the error. Help me please.
1- Here is the working code,try it on The Go Playground:
package main
import (
"encoding/json"
"fmt"
)
func main() {
var d *R
err := json.Unmarshal([]byte(str), &d)
if err != nil {
panic(err)
}
var data R2
data.Count = int(d.Response[0].(float64))
for _, v := range d.Response[1:] {
bs, err := json.Marshal(v)
if err != nil {
panic(err)
}
var d1 *D
err = json.Unmarshal(bs, &d1)
if err != nil {
panic(err)
}
data.Response = append(data.Response, *d1)
}
fmt.Println(data)
}
type R struct {
Response []interface{} `json:"response"`
}
var str = ` {
"response": [
777,
{
"id": 888,
"from_id": 999,
"to_id": 888,
"text": "hello"
},
{
"id": 999,
"from_id": 888,
"to_id": 999,
"text": "goodbye"
}
]
}`
type D struct {
Id int `json:"id"`
FromId int `json:"from_id"`
ToId int `json:"to_id"`
Text string `json:"text"`
}
type R2 struct {
Count int
Response []D
}
output:
{777 [{888 999 888 hello} {999 888 999 goodbye}]}
Your response json is invalid. You use array instead of structure with fields specified in type D.
I have a different approach: https://play.golang.org/p/9Xnxk7tVxE
All you have to do is implement the Unmarshaler interface and add the logic inside the method UnmarshalJSON. It's a similar solution to the other one, but it's more portable since you don't have to do that in the main or external method and it's little more robust :)
package main
import (
"encoding/json"
"fmt"
"log"
)
var j = `{
"response": [
777,
{
"id": 888,
"from_id": 999,
"to_id": 888,
"text": "hello..."
},
{
"id": 999,
"from_id": 888,
"to_id": 999,
"text": "goodbye..."
}
]
}`
type D struct {
Id int `json:"id"`
FromId int `json:"from_id"`
ToId int `json:"to_id"`
Text string `json:"text"`
}
type R struct {
Count int
Response []D
}
func (r *R) UnmarshalJSON(data []byte) error {
var values struct {
Resp []interface{} `json:"response"`
}
if err := json.Unmarshal(data, &values); err != nil {
return err
}
if len(values.Resp) < 1 {
return fmt.Errorf("empty response %v", values.Resp)
}
count, isNumber := values.Resp[0].(float64)
if !isNumber {
return fmt.Errorf("first element has to be a number, we got '%T'", values.Resp[0])
}
r.Count = int(count)
for _, elem := range values.Resp[1:] {
de, err := json.Marshal(elem)
if err != nil {
return err
}
var d D
if err := json.Unmarshal(de, &d); err != nil {
return err
}
r.Response = append(r.Response, d)
}
return nil
}
func main() {
var data = new(R)
if err := json.Unmarshal([]byte(j), &data); err != nil {
log.Fatal(err)
}
fmt.Println(*data)
}

Error unmarshalling JSON in Go that starts with an Array

This is my code
package main
import (
"encoding/json"
"log"
)
type Data struct {
Page int
Pages int
PerPage string
Total int
CountriesList []Country
}
type Country struct {
Id string
Iso string
}
func main() {
body := []byte(`[
{
"page": 1,
"pages": 6,
"per_page": "50",
"total": 256
},
[
{
"id": "ABW",
"iso2Code": "AW"}]]`)
items := make([]Data, 10)
if err := json.Unmarshal(body, &items); err != nil {
log.Fatalf("error %v", err)
}
}
I'm try to unmarshall some JSON and getting the following error:
error json: cannot unmarshal array into Go value of type main.Data
When reading the question, I assumed that there could be multiple Data + list of Country pairs. Here's the solution I ended up with:
package main
import (
"encoding/json"
"fmt"
"log"
)
type Data struct {
Page int
Pages int
PerPage string `json:"per_page, string"`
Total int
}
type Country struct {
Id string
Iso2Code string
}
type DataCountry struct {
Data Data
CountryList []Country
}
func main() {
body := []byte(`[
{
"page": 1,
"pages": 6,
"per_page": "50",
"total": 256
},
[
{
"id": "ABW",
"iso2Code": "AW"}]
]`)
raw := make([]json.RawMessage, 10)
if err := json.Unmarshal(body, &raw); err != nil {
log.Fatalf("error %v", err)
}
sdc := make([]DataCountry, 0)
for i := 0; i < len(raw); i += 2 {
dc := DataCountry{}
data := Data{}
if err := json.Unmarshal(raw[i], &data); err != nil {
fmt.Println("error %v", err)
} else {
dc.Data = data
}
var countries []Country
if err := json.Unmarshal(raw[i+1], &countries); err != nil {
fmt.Println("error %v", err)
} else {
dc.CountryList = countries
}
sdc = append(sdc, dc)
}
fmt.Printf("%v\n", sdc)
}
I found the blog post "Using go to unmarshal json lists with multiple types" very useful for understanding several different options for dealing parsing JSON lists.
package main
import (
"encoding/json"
"fmt"
"log"
)
type Data struct {
Page int
Pages int
Per_Page string
Total int
CountriesList []Country
}
type Country struct {
Id string
Iso2Code string
}
func main() {
body := []byte(`
[
{
"page": 1,
"pages": 6,
"per_page": "50",
"total": 256,
"countrieslist": [
{
"id": "ABW",
"iso2Code": "AW"
}
]
}
]
`)
items := make([]Data, 10)
if err := json.Unmarshal(body, &items); err != nil {
log.Fatalf("error %v", err)
}
fmt.Printf("%#v\n", items)
}
Playground
Output:
[]main.Data{main.Data{Page:1, Pages:6, Per_Page:"50", Total:256, CountriesList:[]main.Country{main.Country{Id:"ABW", Iso2Code:"AW"}}}}
I got some help from #go-nuts on IRC:
package main
import (
"encoding/json"
"fmt"
"log"
)
type T1 struct {
Page, Pages, Total int
PerPage int `json:"per_page,string"`
}
type T2 struct {
ID string
ISO2Code string
}
func main() {
body := []byte(`
[
{
"page": 1,
"pages": 6,
"per_page": "50",
"total": 256
},
[
{
"id": "ABW",
"iso2Code": "AW"
},
{
"id": "AFG",
"iso2Code": "AF"
}
]
]
`)
t1 := T1{}
t2 := []T2{}
if err := json.Unmarshal(body, &[]interface{}{&t1, &t2}); err != nil {
log.Fatalf("error %v", err)
}
fmt.Printf("%#v %#v", t1, t2)
for k, v := range t2 {
fmt.Printf("%v %v\n",k, v.ID)
}
}