admin管理员组文章数量:1415491
Im trying to Unmarshal some JSON data. I think the problem is that the 2 arrays have different fields so do not properly match my struct. Essentially I only need the data from the first item in the array. Is there a way to deal with this. Here's what I have, however it's not throwing any errors but it doesn't seem to be returning anything.
Here's an example in go playground
package main
import (
"encoding/json"
"fmt"
"time"
)
var jsondata = `{
"Secrets": [
{
"name": "default",
"versionInfo": "2025-02-10T20:11:05Z/1",
"lastUpdated": "2025-02-10T20:11:05.512Z",
"secret": {
"@type": "type.googleapis/envoy.extensions.transport_sockets.tls.v3.Secret",
"name": "default",
"tlsCertificate": {
"certificateChain": {
"inlineBytes": "RRVkp2YjNSRApRVEV1WTN=="
},
"privateKey": {
"inlineBytes": "W3JlZFRDSGVkXQ=="
}
}
}
},
{
"name": "default2",
"versionInfo": "2025-02-10T20:11:05Z/3",
"lastUpdated": "2025-02-10T20:11:05.527Z",
"secret": {
"@type": "type.googleapis/envoy.extensions.transport_sockets.tls.v3.Secret",
"name": "ROOTCA",
"validationContext": {
"trustedCa": {
"inlineBytes": "LS0tLS1CRUdJTiBDRVJUSUZJ="
}
}
}
}
]
}`
type model struct {
Name string `json:"name"`
VersionInfo time.Time `json:"versionInfo"`
LastUpdated time.Time `json:"lastUpdated"`
Secret struct {
Type string `json:"@type"`
Name string `json:"name"`
TLSCertificate struct {
CertificateChain struct {
InlineBytes string `json:"inlineBytes"`
} `json:"certificateChain"`
PrivateKey struct {
InlineBytes string `json:"inlineBytes"`
} `json:"privateKey"`
} `json:"tlsCertificate"`
} `json:"secret"`
}
func main() {
input := []byte(jsondata)
//Source := (*json.RawMessage)(&data2)
var Info model
// Notice the dereferencing asterisk *
err := json.Unmarshal(input, &Info)
if err != nil {
//fmt.Printf(err.Error())
panic(err)
}
fmt.Printf("%s", Info.Name)
}
Im trying to Unmarshal some JSON data. I think the problem is that the 2 arrays have different fields so do not properly match my struct. Essentially I only need the data from the first item in the array. Is there a way to deal with this. Here's what I have, however it's not throwing any errors but it doesn't seem to be returning anything.
Here's an example in go playground
package main
import (
"encoding/json"
"fmt"
"time"
)
var jsondata = `{
"Secrets": [
{
"name": "default",
"versionInfo": "2025-02-10T20:11:05Z/1",
"lastUpdated": "2025-02-10T20:11:05.512Z",
"secret": {
"@type": "type.googleapis/envoy.extensions.transport_sockets.tls.v3.Secret",
"name": "default",
"tlsCertificate": {
"certificateChain": {
"inlineBytes": "RRVkp2YjNSRApRVEV1WTN=="
},
"privateKey": {
"inlineBytes": "W3JlZFRDSGVkXQ=="
}
}
}
},
{
"name": "default2",
"versionInfo": "2025-02-10T20:11:05Z/3",
"lastUpdated": "2025-02-10T20:11:05.527Z",
"secret": {
"@type": "type.googleapis/envoy.extensions.transport_sockets.tls.v3.Secret",
"name": "ROOTCA",
"validationContext": {
"trustedCa": {
"inlineBytes": "LS0tLS1CRUdJTiBDRVJUSUZJ="
}
}
}
}
]
}`
type model struct {
Name string `json:"name"`
VersionInfo time.Time `json:"versionInfo"`
LastUpdated time.Time `json:"lastUpdated"`
Secret struct {
Type string `json:"@type"`
Name string `json:"name"`
TLSCertificate struct {
CertificateChain struct {
InlineBytes string `json:"inlineBytes"`
} `json:"certificateChain"`
PrivateKey struct {
InlineBytes string `json:"inlineBytes"`
} `json:"privateKey"`
} `json:"tlsCertificate"`
} `json:"secret"`
}
func main() {
input := []byte(jsondata)
//Source := (*json.RawMessage)(&data2)
var Info model
// Notice the dereferencing asterisk *
err := json.Unmarshal(input, &Info)
if err != nil {
//fmt.Printf(err.Error())
panic(err)
}
fmt.Printf("%s", Info.Name)
}
Share
Improve this question
edited Feb 11 at 11:27
user1513388
asked Feb 11 at 11:17
user1513388user1513388
7,46116 gold badges74 silver badges116 bronze badges
1 Answer
Reset to default 1Your input JSON is an object that has a Secrets
property which is a JSON array. You have to model this with a slice in Go, like this:
type wrapper struct {
Secrets []*model `json:"Secrets"`
}
And inside JSON the versionInfo
is not a valid time (notice the trailing /1
and /3
, use string
and you can process it later:
VersionInfo string `json:"versionInfo"`
Now parsing your input:
input := []byte(jsondata)
var w wrapper
err := json.Unmarshal(input, &w)
if err != nil {
panic(err)
}
for _, model := range w.Secrets {
fmt.Printf("Name: %s, secret name: %s\n", model.Name, model.Secret.Name)
}
This will output (try it on the Go Playground):
Name: default, secret name: default
Name: default2, secret name: ROOTCA
本文标签: goJSON data Unmarshal with diffrent field names in arrayStack Overflow
版权声明:本文标题:go - JSON data Unmarshal with diffrent field names in array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745215683a2648151.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论