简体   繁体   中英

How to unmarshall / store unknown json fields?

I need to Unmarshal json from a 3rd party API. Although I know the response type I need to make sure I don't loose any field that the API may introduce (the api has no documentation) so I'm wondering how can I do that. Ideally I would like to store the unknown fields in an interface{} value and perhaps encode it for later audit. Here is what i've tried so far but it doesn't work( Data field which is "unknown" at compile time is lost during unmarshalling).

Play

package main

import (
    "encoding/json"
    "fmt"
)

type Tweet struct {
    User_id int
    Message string
    Unknown
}
type Unknown map[interface{}]interface{}

func main() {
    // Define an empty interface
    var t Tweet

    // Convert JSON string into bytes
    b := []byte(`{"user_id": 1, "message": "Hello world", "Date": "somerandom date"}`)

    // Decode bytes b into interface i
    json.Unmarshal(b, &t)
    fmt.Println(t)
}

You could use RawMessage as inf suggested. Here's an example using sharktanklabs j2n package .

package main

import (
    "encoding/json"
    "fmt"

    "github.com/sharktanklabs/j2n"
)

type TweetData struct {
    User_id  int
    Message  string
    Overflow map[string]*json.RawMessage `json:"-"`
}

type Tweet struct {
    TweetData
}

func (c *Tweet) UnmarshalJSON(data []byte) error {
    return j2n.UnmarshalJSON(data, &c.TweetData)
}

func (c Tweet) MarshalJSON() ([]byte, error) {
    return j2n.MarshalJSON(c.TweetData)
}

func main() {
    // Define an empty interface
    var t Tweet

    // Convert JSON string into bytes
    b := []byte(`{"user_id": 1, "message": "Hello world", "Date": "somerandom date"}`)

    // Decode bytes b into interface i
    json.Unmarshal(b, &t)
    fmt.Printf("%#v\n", t)
}
// Formatted output: 
//     main.Tweet{TweetData:main.TweetData{
//         User_id:1, 
//         Message:"Hello world",     
//         Overflow:map[string]*json.RawMessage{
//             "user_id":(*json.RawMessage)(0xc82000e340), 
//             "message":(*json.RawMessage)(0xc82000e3c0), 
//             "Date":(*json.RawMessage)(0xc82000e440)}}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM