---
title: "JSON Tricks: Extending an Embedded Marshaler"
url: https://daily.dev/posts/json-tricks-extending-an-embedded-marshaler-akawfzbii
source_url: https://boldlygo.tech/posts/2020-06-26-go-json-tricks-embedded-marshaler
type: article
source: "Boldly Go"
published: 2026-05-31T07:50:11.266Z
updated: 2026-05-31T09:09:42.327Z
tags: ["golang"]
reading_time: 5
upvotes: 0
comments: 0
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# JSON Tricks: Extending an Embedded Marshaler

**[Boldly Go](https://daily.dev/sources/boldlygo)** · 5 min read · 0 upvotes · 0 comments

## Summary

When embedding a struct with a custom MarshalJSON method in Go, the promoted method causes any additional fields on the outer struct to be silently ignored during marshaling. The solution is to define a new MarshalJSON on the outer type that explicitly calls the embedded type's MarshalJSON, marshals only the new fields into a separate JSON object using a local type, then splices the two JSON byte slices together by replacing the opening brace of the second with a comma and appending it to the first (minus its closing brace). This avoids duplicating the embedded type's logic while correctly including all fields in the output.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://boldlygo.tech/posts/2020-06-26-go-json-tricks-embedded-marshaler>

## Similar posts on daily.dev

- [Lazy attribute evaluation for JSONHandler](https://daily.dev/posts/lazy-attribute-evaluation-for-jsonhandler-qc9mwy8bm) · Boldly Go · 15 upvotes · 0 comments
- [A new experimental Go API for JSON](https://daily.dev/posts/a-new-experimental-go-api-for-json-u5mtnrdxh) · Go · 34 upvotes · 0 comments

---

Tags: [#golang](https://daily.dev/tags/golang)

[View this post on daily.dev](https://daily.dev/posts/json-tricks-extending-an-embedded-marshaler-akawfzbii)
