Unmarshaling response into proto structs

Hi There,

Any chance to get the code below to work without new structs or modifying existing proto definitions ?
I’m currently getting the error “json: cannot unmarshal array into Go struct field Root.data of type pb.Root”

// unmarshaling query result
tmp := pb.Root{}
err := json.Unmarshal(resp.Json, &tmp)

Here are the query / proto definitions :

// query
// {
//     data(func: eq(name, "my name")){
//         name
//         field1
//         field2
//     }
// }

// pb definition

// message MyStruct {
//     string name = 1;
//     string field1 = 2;
//     string field2 = 3;
// }

// message Root { 
//     MyStruct data = 1;
// }

Thanks !

package main

import (
    "encoding/json"
    "fmt"

    "github.com/golang/protobuf/proto"
)

// MyStruct from your proto definition
type MyStruct struct {
    Name   string `json:"name" protobuf:"bytes,1,opt,name=name"`
    Field1 string `json:"field1" protobuf:"bytes,2,opt,name=field1"`
    Field2 string `json:"field2" protobuf:"bytes,3,opt,name=field2"`
}

// Root from your proto definition
type Root struct {
    Data *MyStruct `json:"data" protobuf:"bytes,1,opt,name=data"`
}

// Temporary struct to hold the array of MyStruct returned by Dgraph
type TempRoot struct {
    Data []MyStruct `json:"data"` // Matches the structure from Dgraph's output
}

func main() {
    // Example JSON response from Dgraph
    jsonData := []byte(`
    {
        "data": [
            {
                "name": "my name",
                "field1": "value1",
                "field2": "value2"
            }
        ]
    }`)

    // Unmarshal into temp structure
    var temp TempRoot
    err := json.Unmarshal(jsonData, &temp)
    if err != nil {
        fmt.Println("Error unmarshaling JSON:", err)
        return
    }

    // If we expect only one result, take the first element
    if len(temp.Data) > 0 {
        root := Root{
            Data: &temp.Data[0], // Reference to the first (or only) element in the array
        }

        // If you need to work with this as a proto message:
        protoRoot := &Root{
            Data: &MyStruct{
                Name:   root.Data.Name,
                Field1: root.Data.Field1,
                Field2: root.Data.Field2,
            },
        }

        // Use protoRoot as needed, it's now correctly formatted according to your proto definition
        fmt.Printf("Unmarshaled Proto: %+v\n", protoRoot)
    } else {
        fmt.Println("No data found in the response")
    }
}