Decode JSON with Go - json

I have a reaction struct like this
type Reaction struct {
Id uint `json:"id" form:"id"`
ReactionType uint `json:"reactionType" form:"reactionType"`
PostId uint `json:"postId" form:"postId"`
ReactorId uint `json:"reactorId" form:"reactorId"`
CreatedAt string `json:"createdAt" form:"createdAt"`
UpdatedAt string `json:"updatedAt" form:"createdAt"`
}
And I have a function that consumes an API that should return a slice of Reaction
var myClient = &http.Client{Timeout: 7 * time.Second}
func getJson(url string, result interface{}) error {
req, _ := http.NewRequest("GET", url, nil)
resp, err := myClient.Do(req)
if err != nil {
return fmt.Errorf("cannot fetch URL %q: %v", url, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected http GET status: %s", resp.Status)
}
err = json.NewDecoder(resp.Body).Decode(result)
if err != nil {
return fmt.Errorf("cannot decode JSON: %v", err)
}
return nil
}
But somehow it failed to display the array/slice of objects that I want to retrieve, I got no data at all. Where did I miss?
func main(){
..
..
var reactList []Reaction
getJson("http://localhost:80/reactions", reactList)
for _, r := range reactList {
fmt.Print(r.ReactionType)
}
}
and this is the original responses
[
{
"id": 55,
"reactionType": 5,
"reactorId": 2,
"postId": 4,
"createdAt": "2017-11-18 14:23:29",
"updatedAt": ""
},
{
"id": 56,
"reactionType": 5,
"reactorId": 3,
"postId": 4,
"createdAt": "2017-11-18 14:23:42",
"updatedAt": ""
},
{
"id": 57,
"reactionType": 4,
"reactorId": 4,
"postId": 4,
"createdAt": "2017-11-18 14:23:56",
"updatedAt": ""
}
]

As pointed out in the comments you need to pass a pointer to getJson so that it can actually modify the contents of slice.
getJson("http://localhost:80/reactions", &reactList)
This is a close representation of what you have https://play.golang.org/p/_PnNK64giE, just see where things aren't in place in your case.

Check this playground: https://play.golang.org/p/VjocUtiDRN
Maybe this is what you want. So you should make your function getJson(btw getJSON is better name in Golang naming convention) takes []Reaction parameter, instead of interface{}. Then you can unmarshal your array-like response into slice of Reactions.

The main error here is that you're passing result without pointer. Should be:
getJson("http://localhost:80/reactions", &reactList)

Related

`json.Marshal()` converts an array field of a struct to map which it shouldn't

I want to serialize object of types.Project from github.com/compose-spec into JSON
However, a field of ths struct which is supposed to be array, is always serialized into map.
package main
import (
"encoding/json"
types "github.com/compose-spec/compose-go/types"
)
func main() {
out := types.Project{
Name: "foo",
Services: []types.ServiceConfig{ // <- notice this field is an array
{
Name: "bar",
Image: "hello-world",
},
},
}
buf, err := json.Marshal(out)
if err != nil {
panic(err)
}
println(string(buf)) // <- notice the Services field now a map, which is incorrect!
var in types.Project
if err := json.Unmarshal(buf, &in); err != nil {
panic(err)
}
}
The code fails to run:
{"name":"foo","services":{"bar":{"command":null,"entrypoint":null,"image":"hello-world"}}}
panic: json: cannot unmarshal object into Go struct field Project.services of type types.Services
goroutine 1 [running]:
main.main()
/tmp/sandbox1081064727/prog.go:29 +0x168
The out object is serialized as
{
"name": "foo",
"services": {
"bar": {
"command": null,
"entrypoint": null,
"image": "hello-world"
}
}
}
which should really be something like
{
"name": "foo",
"services": [
{
"name": "bar",
"command": null,
"entrypoint": null,
"image": "hello-world"
}
]
}
The behavior you're seeing is correct with respect to the compose file specification, which says:
A Compose file MUST declare a services root element as a map whose keys are string representations of service names, and whose values are service definitions.
The transformation of the list to a map is implemented by the custom marshalers in compose-go/types.go:
// MarshalYAML makes Services implement yaml.Marshaller
func (s Services) MarshalYAML() (interface{}, error) {
services := map[string]ServiceConfig{}
for _, service := range s {
services[service.Name] = service
}
return services, nil
}
// MarshalJSON makes Services implement json.Marshaler
func (s Services) MarshalJSON() ([]byte, error) {
data, err := s.MarshalYAML()
if err != nil {
return nil, err
}
return json.MarshalIndent(data, "", " ")
}

Golang unmarshaling JSON to protobuf generated structs

I would like to reqeive a JSON response withing a client application and unmarshal this response into a struct. To ensure that the struct stays the same accross all client apps using this package, I would like to define the JSON responses as protobuf messages. I am having difficulties unmarshaling the JSON to the protobuf generated structs.
I have the following JSON data:
[
{
"name": "C1",
"type": "docker"
},
{
"name": "C2",
"type": "docker"
}
]
I have modeled my protobuf definitions like this:
syntax = "proto3";
package main;
message Container {
string name = 1;
string type = 2;
}
message Containers {
repeated Container containers = 1;
}
Using this pattern with structs normaly works, but for some reason using these proto definitions causes issues. The below code demonstrates a working and a non-working example. Although one of the versions work, I am unable to use this solution, since []*Container does not satisfy the proto.Message interface.
package main
import (
"encoding/json"
"fmt"
"strings"
"github.com/gogo/protobuf/jsonpb"
)
func working(data string) ([]*Container, error) {
var cs []*Container
return cs, json.Unmarshal([]byte(data), &cs)
}
func notWorking(data string) (*Containers, error) {
c := &Containers{}
jsm := jsonpb.Unmarshaler{}
if err := jsm.Unmarshal(strings.NewReader(data), c); err != nil {
return nil, err
}
return c, nil
}
func main() {
data := `
[
{
"name": "C1",
"type": "docker"
},
{
"name": "C2",
"type": "docker"
}
]`
w, err := working(data)
if err != nil {
panic(err)
}
fmt.Print(w)
nw, err := notWorking(data)
if err != nil {
panic(err)
}
fmt.Print(nw.Containers)
}
Running this gives the following output:
[name:"C1" type:"docker" name:"C2" type:"docker" ]
panic: json: cannot unmarshal array into Go value of type map[string]json.RawMessage
goroutine 1 [running]:
main.main()
/Users/example/go/src/github.com/example/example/main.go:46 +0x1ee
Process finished with exit code 2
Is there a way to unmarshal this JSON to Containers? Or alternatively, make []*Container to satisfy the proto.Message interface?
For the message Containers, i.e.
message Containers {
repeated Container containers = 1;
}
The correct JSON should look like:
{
"containers" : [
{
"name": "C1",
"type": "docker"
},
{
"name": "C2",
"type": "docker"
}
]
}
If you cannot change the JSON then you can utilize the func that you've created
func working(data string) ([]*Container, error) {
var cs []*Container
err := json.Unmarshal([]byte(data), &cs)
// handle the error here
return &Containers{
containers: cs,
}, nil
}
You should use NewDecoder to transfer the data to jsonDecoder and then traverse
the array.The code is this
func main() {
data := `
[
{
"name": "C1",
"type": "docker"
},
{
"name": "C2",
"type": "docker"
}
]`
jsonDecoder := json.NewDecoder(strings.NewReader(data))
_, err := jsonDecoder.Token()
if err != nil {
log.Fatal(err)
}
var protoMessages []*pb.Container
for jsonDecoder.More() {
protoMessage := pb.Container{}
err := jsonpb.UnmarshalNext(jsonDecoder, &protoMessage)
if err != nil {
log.Fatal(err)
}
protoMessages = append(protoMessages, &protoMessage)
}
fmt.Println("%s", protoMessages)
}

Declaring a struct for json objects

I have a JSON response object from a API like that:
[
{
"peoples": {
"1": {"name": "Jhon", "age": 123},
"2": {"name": "Jhon", "age": 123},
"3": {"name": "Jhon", "age": 123},
"4": {"name": "Jhon", "age": 123},
"_object": true,
"_timestamp": "2020-08-05T07:05:55.509Z",
"_writable": false
}
}
]
The parameters: peoples, _object, _timestamp and _writable is fixed. The dynamic values are the 1,2,3,4...n parameters.
The qty of peoples in that struct can be more then 4 or can be 1. Have any elegant solution for create a Struct object or a json.Unmarshal for that?
Borrowing the input example from Sarath Sadasivan Pillai (see comment), here (link to Playground example) is a way to do it with map[string]json.RawMessage and a custom unmarshal function:
package main
import (
"encoding/json"
"fmt"
"strconv"
"time"
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
type Keywords struct {
Object bool `json:"_object"`
Timestamp time.Time `json:"_timestamp"`
Writable bool `json:"_writable"`
}
type Decoded struct {
People map[string]Person // or perhaps just []Person
Info Keywords
}
var input []byte = []byte(`{
"1": {"name": "Jhon", "age": 123},
"2": {"name": "Jhon", "age": 123},
"3": {"name": "Jhon", "age": 123},
"4": {"name": "Jhon", "age": 123},
"_object": true,
"_timestamp": "2020-08-05T07:05:55.509Z",
"_writable": false
}`)
// Unmarshal json in the input format outlined by
// the example above: a map of numeric strings to Name/Age pair,
// plus some keywords.
func (d *Decoded) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &d.Info); err != nil {
return err
}
var m map[string]json.RawMessage
if err := json.Unmarshal(data, &m); err != nil {
return err
}
if d.People == nil {
d.People = make(map[string]Person)
}
for k := range m {
// This is the hard part: you must choose how
// to decide whether to decode this as a Person.
// Here, we use strconv.Atoi() as in the example
// by Sarath Sadasivan Pillai, but there are as
// many options as you can think of.
//
// For instance, another method would be to try
// decoding the json.RawMessage as a Person. It's
// also not clear whether the numeric values imply
// some particular ordering, which maps discard.
// (If these come out in order, that's just luck.)
if _, err := strconv.Atoi(k); err != nil {
continue
}
var p Person
if err := json.Unmarshal(m[k], &p); err != nil {
return err
}
d.People[k] = p
}
return nil
}
func main() {
var x Decoded
err := json.Unmarshal(input, &x)
if err != nil {
fmt.Printf("failed: %v\n", err)
} else {
fmt.Printf("succeeded:\n%#v\n", x.Info)
for k := range x.People {
fmt.Printf("person %q = %#v\n", k, x.People[k])
}
}
}

Unmarshalling complex json in Go

So I am trying to fetch the analytics of an app by pinging and endpoint. I make the GET request which is successfull (no errors there) but I am unable to decode the JSON
I need to to decode the following json into structs
{
"noResultSearches": {
"results": [
{
"count": 1,
"key": "\"note 9\""
},
{
"count": 1,
"key": "nokia"
}
]
},
"popularSearches": {
"results": [
{
"count": 4,
"key": "6"
},
{
"count": 2,
"key": "\"note 9\""
},
{
"count": 1,
"key": "nokia"
}
]
},
"searchVolume": {
"results": [
{
"count": 7,
"key": 1537401600000,
"key_as_string": "2018/09/20 00:00:00"
}
]
}
}
For which I am using the following structs
type analyticsResults struct {
Count int `json:"count"`
Key string `json:"key"`
}
type analyticsVolumeResults struct {
Count int `json:"count"`
Key int64 `json:"key"`
DateAsStr string `json:"key_as_string"`
}
type analyticsPopularSearches struct {
Results []analyticsResults `json:"results"`
}
type analyticsNoResultSearches struct {
Results []analyticsResults `json:"results"`
}
type analyticsSearchVolume struct {
Results []analyticsVolumeResults `json:"results"`
}
type overviewAnalyticsBody struct {
NoResultSearches analyticsNoResultSearches `json:"noResultSearches"`
PopularSearches analyticsPopularSearches `json:"popularSearches"`
SearchVolume analyticsSearchVolume `json:"searchVolume"`
}
I make a GET request to an endpoint and then use the response body to decode the json but I get an error. Following is a part of the code that stays in my ShowAnalytics function
func ShowAppAnalytics(app string) error {
spinner.StartText("Fetching app analytics")
defer spinner.Stop()
fmt.Println()
req, err := http.NewRequest("GET", "<some-endpoint>", nil)
if err != nil {
return err
}
resp, err := session.SendRequest(req)
if err != nil {
return err
}
spinner.Stop()
var res overviewAnalyticsBody
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&res)
if err != nil {
return err
}
fmt.Println(res)
return nil
}
json: cannot unmarshal array into Go struct field
overviewAnalyticsBody.noResultSearches of type
app.analyticsNoResultSearches
What am I doing wrong here? Why do I get this error?
EDIT: After you edited, your current code works as-is. Check it out here: Go Playground.
Original answer follows.
There is some inconsistency between the code you posted and the error you get.
I tried it on the Go Playground (here's your version), and I get the following error:
json: cannot unmarshal number into Go struct field analyticsVolumeResults.key of type string
We get this error because in the JSON searchVolume.results.key is a number:
"key": 1537401600000,
And you used string in the Go model:
Key string `json:"key"`
If we change it to int64:
Key int64 `json:"key"`
It works, and prints (try it on the Go Playground):
{{[{1 "note 9"} {1 nokia}]} {[{4 6} {2 "note 9"} {1 nokia}]} {[{7 1537401600000 2018/09/20 00:00:00}]}}
If that key may sometimes be a number and sometimes a string, you may also use json.Number in the Go model:
Key json.Number `json:"key"`

Having trouble decoding JSON response [duplicate]

This question already has answers here:
My structures are not marshalling into json [duplicate]
(3 answers)
Closed 7 years ago.
This is my first attempt at Go and I feel I'm missing something important here. Trying to decode a JSON message from a webservice but the output I'm getting is:
{response:{requests:[]}}
All I'm really interested in is the data within the request node. My for-loop obviously isn't getting called because the array is empty. I feel like my structs need to be declared exactly as they appear in the webservice?
Sample JSON:
{
"response": {
"requests": [
{
"request": {}
},
{
"request": {
"id": 589748,
"image_thumbnail": "",
"description": "Blah blah",
"status": "received",
"user": "test"
}
}
],
"count": "50",
"benchmark": 0.95516896247864,
"status": {},
"debug": {}
}
}
type Request struct {
id int `json:"id"`
description string `json:"description"`
user string `json:"user"`
}
type Requests struct {
request Request `json:"request"`
}
type Response struct {
requests []Requests `json:"requests"`
}
type RootObject struct {
response Response `json:"response"`
}
url := "<webservice>"
req, err := http.NewRequest("GET", url, nil)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
var r RootObject
decoder := json.NewDecoder(resp.Body)
decoder.Decode(&r)
fmt.Printf("%+v", r)
for _, req := range r.response.requests {
fmt.Printf("%d = %s\n", req.request.id, req.request.user)
}
Field names need to begin with upper case character to be exported identifiers.