Jukurrpa Jukurrpa - 6 months ago 9
JSON Question

Aggregating JSON objects in Go

I'm making a Go service that gathers JSON objects from different sources and aggregates them in a single JSON object.

I was wondering if there was any way to aggregate the child objects without having to unmarshal and re-marshal them again or having to manually build a JSON string.

I was thinking of using a

struct
containing the already marshalled parts, such as this:

type Event struct {
Place string `json:"place"`
Attendees string `json:"attendees"`
}


Where
Place
and
Attendees
are JSON strings themselves. I'd like to somehow mark them as "already marshalled" so they don't end up as escaped JSON strings but get used as is instead.

Is there any way to achieve this?

Answer

Yes, you can use a custom type that implements Marshaler interface.

https://play.golang.org/p/YB_eKlfOND

package main

import (
    "fmt"
    "encoding/json"
)

type Event struct {
    Place     RawString  `json:"place"`
    Attendees RawString  `json:"attendees,omitempty"`
}

type RawString string

func (s RawString) MarshalJSON() ([]byte, error) {
    return []byte(s), nil
}

func main() {
    event := Event{
        Place: RawString(`{"name":"Paris"}`),
        Attendees: RawString(`[{"name":"John"}, {"name":"Juli"}]`),
    }
    s, err := json.Marshal(event)
    fmt.Println(fmt.Sprintf("event: %v; err: %v", string(s), err))
}