Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON marshal empty arrays as "[]" #31811

Closed
mcandre opened this issue May 2, 2019 · 3 comments
Closed

JSON marshal empty arrays as "[]" #31811

mcandre opened this issue May 2, 2019 · 3 comments

Comments

@mcandre
Copy link

mcandre commented May 2, 2019

golang's stdlib encoding/json library currently marshals empty arrays as null rather than [], causing downstream JSON parsing to often fail.

Practically, many JSON parsers reject raw arrays that are not wrapped in some { blah: array } object structure. I think that is wasteful, but if encoding/json were to behave more predictably, it could have at least reported an error when Marshal() is passed an array instead of a struct / hashmap. Instead we get null for empty arrays, which is not a value anyone but old LISPers from the 50's would expect.

As a workaround, some Go users are substituting https://github.com/helloeave/json

Perhaps we can bring this improvement back to encoding/json, without breaking backwards compatibility, by introducing new methods and deprecating old methods.

@mvdan
Copy link
Member

mvdan commented May 3, 2019

Dup of #27589.

@mvdan mvdan closed this as completed May 3, 2019
@gopherbot
Copy link
Contributor

Change https://golang.org/cl/205897 mentions this issue: encoding/json: Add nilasempty option

@MilindKhairnar
Copy link

This issue only happens if array is primitive (e.g. string array). it worked fine for non-primitive array

@golang golang locked and limited conversation to collaborators Jan 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants