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

proposal: encoding/json: using inspired jq syntax to unmarshal/marshall JSON #50407

Closed
rramiachraf opened this issue Jan 2, 2022 · 1 comment

Comments

@rramiachraf
Copy link

rramiachraf commented Jan 2, 2022

Let's imagine we have this JSON:

{
	"name": {
		"first": "John",
		"last": "Doe"
	}
}

And a developer wants to unmarshal it into this struct:

type person struct {
          FirstName string
          LastName  string
}

I think using something like the syntax below would make doing the decoding process a lot easier without the need of a custom unmarshaler or using any external packages and would be a life saver when you have big JSON strings with dozens of nested data.

type person struct {
          FirstName string `json:"name.firstname"`
          LastName  string `json:"name.lastname"`
}
@gopherbot gopherbot added this to the Proposal milestone Jan 2, 2022
@rramiachraf rramiachraf changed the title proposal: encoding/json: using jq syntax to unmarshal/marshall json tk proposal: encoding/json: using jq syntax to unmarshal/marshall json Jan 2, 2022
@rramiachraf rramiachraf changed the title proposal: encoding/json: using jq syntax to unmarshal/marshall json proposal: encoding/json: using jq syntax to unmarshal/marshall JSON Jan 2, 2022
@rramiachraf rramiachraf changed the title proposal: encoding/json: using jq syntax to unmarshal/marshall JSON proposal: encoding/json: using inspired jq syntax to unmarshal/marshall JSON Jan 2, 2022
@mdlayher
Copy link
Member

mdlayher commented Jan 2, 2022

The equivalent that works today is:

type person struct {
    Name struct {
        First string `json:"first"`
        Last string `json:"last"`
    } `json:"name"`
}

Why does this need to be part of the standard library? Couldn't this syntax be implemented in a third party package? See https://go.dev/doc/faq#x_in_std.

@golang golang locked and limited conversation to collaborators Jan 2, 2023
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

3 participants