1

I am trying to convert this json-format string into an actual json object in GOLANG.

{"predictions": [{"predictions": [4492.0]}, {"predictions": [4515.84716796875]}, {"predictions": [4464.86083984375]}]}

Basically, it is a dictionary with a key "predictions" and the value es an array of dictionaries (each dictionary is has as a key "predictions" and a value an 1-element float array. I created two structure (one for the first dictionary and the other one for the array of dictionaries), but I can't fit the string json to my structure. I am not sure what I am missing

package main

import (
    "encoding/json"
    "fmt"
)


type dataPredictions struct {
    SinglePredictions *SinglePredictions `json:"predictions"`
}

type SinglePredictions struct {
    Predictions []map[string]int `json:predictions`
}

func main() {

    s := `{"predictions": [{"predictions": [4492.0]}, {"predictions": [4515.84716796875]}, {"predictions": [4464.86083984375]}]}`

    data := &dataPredictions{
        SinglePredictions: &SinglePredictions{},
    }
    err := json.Unmarshal([]byte(s), data)
  s2, _ := json.Marshal(data)
    fmt.Println(err)
    fmt.Println(data.SinglePredictions)
    fmt.Println(string(s2))

}

The error I get is below.

json: cannot unmarshal array into Go struct field dataPredictions.predictions of type main.SinglePredictions
1
  • Note that you aren't unmarshalling into a "JSON object". The original string is a JSON object, you're unmarshalling it into a Go struct. Commented Jun 20, 2018 at 19:34

2 Answers 2

1

There are basically two mistakes. The first is that you didn't define SinglePredictions as a slice, which is why you got the error in the first place, and then you used the map when you simply needed to pass down []float64.

package main

import (
    "encoding/json"
    "fmt"
)

type dataPredictions struct {
    SinglePredictions []*SinglePredictions `json:"predictions"`
}

type SinglePredictions struct {
    Predictions []float64 `json:"predictions"`
}

func main() {
    s := `{"predictions": [{"predictions": [4492.0]}, {"predictions": [4515.84716796875]}, {"predictions": [4464.86083984375]}]}`
    // Note that you don't need to assign values manually - json.Unmarshal
    // will do that for you.
    data := &dataPredictions{}
    err := json.Unmarshal([]byte(s), data)
    s2, _ := json.Marshal(data)
    fmt.Println(err)
    fmt.Println(data.SinglePredictions)
    fmt.Println(string(s2))

}

The mistake you made seems that to have been thinking that Go would have unmarshalled the first array into dataPredictions.SinglePredictions.Predictions. But in dataPredictions you have a field which selects the "predictions" key in the topmost object, and its value is then passed to unmarshal into a *SinglePredictions

Sign up to request clarification or add additional context in comments.

2 Comments

Why do you put an asterisk (*) in []*SinglePredictions? it seems to work fine without it, what's the difference?
There is a difference, but in this context it does not really matter and you can leave it out. I just copied it from the code of your question.
1

Try this:

package main

import (
    "encoding/json"
    "fmt"
)


type dataPredictions struct {
    SinglePredictions []SinglePredictions `json:"predictions"`
}

type SinglePredictions struct {
    Predictions []float64 `json:"predictions"`
}

func main() {
    s := []byte(`{"predictions": [{"predictions": [4492.0]}, {"predictions": [4515.84716796875]}, {"predictions": [4464.86083984375]}]}`)

    data := dataPredictions{}
    err := json.Unmarshal(s, &data)
    s2, _ := json.Marshal(data)
    fmt.Println(err)
    fmt.Println(data.SinglePredictions)
    fmt.Println(string(s2))
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.