Skip to content

encoding/json: use the Error method to marshal an error value? #5161

Closed

Description

@orofarne
What steps will reproduce the problem?
http://play.golang.org/p/epXpwk0s4s

What is the expected output?
Something like this:
http://play.golang.org/p/Dk0l6fTj01

What do you see instead?
{"bar":{},"foo":{}}

Which compiler are you using (5g, 6g, 8g, gccgo)?


Which operating system are you using?
Linux (Ubuntu 12.04, 3.2.0-38-generic)

Which version are you using?  (run 'go version')
go version go1.0.3
go version devel +5260abd6df41 Sat Mar 30 19:05:00 2013 +0800 linux/amd64

Please provide any additional information below.
I think it's not intuitive behaviour. fmt prints errors without problems, why json not?

Activity

  1. remyoudompheng commented on Mar 30, 2013

    @remyoudompheng
    Contributor

    Comment 1:

    See also issue #3353.
  2. gopherbot commented on Jul 27, 2013

    @gopherbot
    Contributor

    Comment 2 by pongad:

    I believe this is the expected behavior of json.Marshal(interfaceValue). According to
    the documentation, if the argument is an interface value, it is encodes the value of
    that interface.
    The implementation of fmt.Errorf uses errors.errorString type. The type is a struct with
    one unexported member 's' which holds the error. Since the member is unexported,
    json.Marshal does not care to encode it.
    A possible workaround is to create a new error type that both satisfies "error" and
    "json.Marshaler" like 
    http://play.golang.org/p/NrtoYsGzij
  3. adg commented on Jul 29, 2013

    @adg
    Contributor

    Comment 3:

    Not sure what the right move is, here. Or even if we can fix it at all. There may be
    code that relies on the JSON marhsaller *not* using the Error method to marshal a value.

    Labels changed: added priority-later, removed priority-triage.

    Status changed to Thinking.

  4. rsc commented on Nov 27, 2013

    @rsc
    Contributor

    Comment 4:

    Labels changed: added go1.3maybe.

  5. rsc commented on Dec 4, 2013

    @rsc
    Contributor

    Comment 5:

    Labels changed: added release-none, removed go1.3maybe.

  6. rsc commented on Dec 4, 2013

    @rsc
    Contributor

    Comment 6:

    Labels changed: added repo-main.

  7. added this to the Unplanned milestone on Apr 10, 2015
  8. dimandzhi commented on Mar 16, 2021

    @dimandzhi

    Hi! I hate to be the one who breaks the almost-a-decade silence, but after several years of using golang I've stumbled uppon a piece of code that uses JSON marshaling of an error interface value. Guess, it is a rare scenario. 😄 Well, result of {"err": {}} wasn't very informative. I realise, that errors interface puprpose is to provide a method to give stringified message of error (with according format for logging even), which doesn't mean it's meant to be marshaled into JSON the same way.
    I think adding a configuration option (not default) to json.Encoder to enable using Error() method to encode value if there is no MarshalJSON() method will help everyone to achieve desired behavior without the need of creating a special type for that.

  9. 1 remaining item

  10. lrweck commented on Jul 3, 2023

    @lrweck

    just stumbled on this too. I guess changing it now would be a breaking change

  11. mitar commented on Aug 22, 2023

    @mitar
    Contributor

    The gitlab.com/tozd/go/errors error package provides JSON marshal of errors which uses some standard interfaces to extract data from the error.

  12. mitar commented on Aug 22, 2023

    @mitar
    Contributor

    But I would suggest that any error marshaling in Go has at least this basic structure: {"error": "...error message"}.

  13. dsnet commented on Oct 6, 2023

    @dsnet
    Member

    Hi all, we kicked off a discussion for a possible "encoding/json/v2" package that addresses the spirit of this proposal.

    I don't think we should provide native support for error types since the JSON representation is only unidirectional (i.e., we can marshal, but can't unmarshal). That said, with #5901, you should be able to accomplish to general gist of what's being asked for here. See this example.

  14. dsnet commented on Aug 20, 2026

    @dsnet
    Member

    Closing as I don't believe this can practically be implemented by default as it's only a unidirectional operation. There is no equivalent unmarshal operation since we do not know the concrete type to use. However, for the marshal case, you can now use MarshalFunc to specify how to marshal error types.

  15. mitar commented on Aug 21, 2026

    @mitar
    Contributor

    I think main issue is that by default error interface without any additional marshaler interface, gets marshaled to {} when it could be nicely marshaled to {"error": "...error message"}. So if error value does also not implement any of other marshalling interfaces, why not marshal to {"error": "...error message"}?

  16. dsnet commented on Aug 21, 2026

    @dsnet
    Member

    That doesn't resolve my primary concern about this being a unidirectional operation.

    Also, the representation as {"error": "...error message"} is an arbitrary choice of representation. Why not just "... error message"? The former can lead to redundant {"error": ...} nesting if the outer layer already puts the error in a field. Both are valid representations depending on usage. Rather than choosing one, we explicitly avoid it and let the user choose one with the new MarshalFunc or MarshalToFunc feature.

    The v2 docs contains an example of how to serialize errors: https://pkg.go.dev/encoding/json/v2#example-WithMarshalers-Errors

  17. mitar commented on Aug 21, 2026

    @mitar
    Contributor

    Thanks! This is great to know!

    And thank you for all amazing work you have done on this v2!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Thinkingv2An incompatible library change

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址