Go JSON parsing

719 views Asked by At

I'm new to Go. I have been trying to get the photos from Flickr using their API but I'm facing an issue parsing the JSON response.

I have been writing the server in Go to handle the web service calls. My Output looks something like this:

{
    "photos": {
        "page": 1,
        "pages": 3583,
        "perpage": 100,
        "total": "358260",
        "photo": [
            {
                "id": "18929318980",
                "owner": "125299498@N04",
                "secret": "505225f721",
                "server": "469",
                "farm": 1,
                "title": "❤️ Puppy Dog Eyes❤️ #Cute #baby #havanese #puppy #love #petsofinstagram #akc #aplacetolovedogs #all_little_puppies #americankennelclub #beautiful #bestanimal #puppies #cutestdogever #dog #doglife #doglover #dogoftheday",
                "ispublic": 1,
                "isfriend": 0,
                "isfamily": 0
            },
            {
                "id": "18930020399",
                "owner": "125421155@N06",
                "secret": "449f493ebc",
                "server": "496",
                "farm": 1,
                "title": "Titt tei hvem er du for en liten tass  Osvald og King  #cat#kitten #bordercollie #puppy#dog",
                "ispublic": 1,
                "isfriend": 0,
                "isfamily": 0
            },
            {
                "id": "18929979989",
                "owner": "131975470@N02",
                "secret": "7da344edcb",
                "server": "498",
                "farm": 1,
                "title": "Shame, Shame",
                "ispublic": 1,
                "isfriend": 0,
                "isfamily": 0
            }
             ]
    },
    "stat": "ok"
}

When I'm trying to run the code it's showing:

cannot use jsonData.Photos.Photo[i].Id (type int) as type []byte in argument to w.Write

My code is given below:

package main

import(
        "os"
        "fmt"
        "log"
        "net/http"
        "io/ioutil"
        "encoding/json"
        "github.com/gorilla/mux"

)
type Result struct {
        Photos struct {
        Page int `json: "page"`
        Pages int `json: "pages"`
        PerPage int `json: "perpage"`
        Total int `json: "total"`
        Photo []struct {
            Id int `json: "id"`
            Owner string `json: "owner"`
            Secret string `json: "secret"`
            Server int `json: "server"`
            Farm int `json: "farm"`
            Title string `json: "title"`
            IsPublic int `json: "ispublic"`
            IsFriend int `json: "isfriend"`
            IsFamily int `json: "isfamily`
            } `json: "photo"`
    } `json: "photos"`
    Stat string `json: "stat"`
}

func main() {

    router := mux.NewRouter().StrictSlash(true);
    router.HandleFunc("/Index", Index)
    log.Fatal(http.ListenAndServe(":8084", router))
}

func Index(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json");
    url := "https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=6b54d86b4e09671ef6a2a8c02b7a3537&text=cute+puppies&format=json&nojsoncallback=1"
    res, err := http.Get(url)
     if err != nil{
        fmt.Printf("%s", err)
        os.Exit(1)
     }
    body, err := ioutil.ReadAll(res.Body)
    if err != nil{
        fmt.Printf("%s", err)
        os.Exit(1)
     }

     jsonData := &Result{}
     err = json.Unmarshal([]byte(body), &jsonData)
     for i := 0;i < len(jsonData); i++ {
        w.Write(jsonData.Photos.Photo[i].Id)
     }
}
1

There are 1 answers

8
evanmcdonnal On BEST ANSWER

The problem isn't with your model, that works just fine. You've already finished deserialzing (without errors) when the error occurs which is on this line; w.Write(jsonData.Photos.Photo[i].Id). You're passing an int when it needs to be a byte array.

This answer explains how to do the conversion; Convert an integer to a byte array

So, to make your code into some working form;

import "encoding/binary"

buffer := make([]byte, 4)
for i := 0;i < len(jsonData.Photos.Photo); i++ {
        binary.LittleEndian.PutUint32(buffer, jsonData.Photos.Photo[i].Id)
        w.Write(buffer)
     }

Note that is writing the binary representation of your value as an unsigned 32 bit int with little endian bit order. That may not work for you. I can't say what will so you gotta make some decisions there like what bit order you want, if you need signed vs unsigned ect.

EDIT: To make the above work I guess you gotta do a cast from int to uint32 as well. Looking more closely at the answer I linked to you can do this instead which is cleaner/simpler imo.

import "strconv"

for _, p := range jsonData.Photos.Photo {
            w.Write([]byte(strconv.Itoa(p.Id)))
         }

Second EDIT: There's many ways to convert an int to binary. Another good option would be; func Write(w io.Writer, order ByteOrder, data interface{}) error

I believe in your code you could use that like;

  import "encoding/binary"

  for i := range jsonData.Photo.Photos {
            binary.Write(w, binary.LittleEndian, jsonData.Photos.Photo[i].Id)
         }

http://golang.org/pkg/encoding/binary/#Write

EDIT: Updated the three examples to have working for loops of different varieties.