-
Notifications
You must be signed in to change notification settings - Fork 11
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
Is there a way to marshal de JSON #23
Comments
@MrMarble marshmallow should be able to help with partial handling of JSON objects. Can you provide more input on what exactly you're trying to achieve? func main() {
file, err := os.Open("input.json")
if err != nil {
panic(err)
}
data, err := io.ReadAll(file)
if err != nil {
panic(err)
}
err = file.Close()
if err != nil {
panic(err)
}
dataAsStruct := someStruct{}
dataAsMap, err := marshmallow.Unmarshal(data, &dataAsStruct)
if err != nil {
panic(err)
}
// process dataAsStruct/dataAsMap
output, err := json.Marshal(dataAsMap) // note we write back dataAsMap. this depends on your exact use case. also note we use json.Marshal but we can also use json.MarshalIndent(data, "", "\t") if you want to prettify the output
if err != nil {
panic(err)
}
err = os.WriteFile("output.json", output, 0644)
if err != nil {
panic(err)
}
} You can share your exact use case and needs to further clarify if marshmallow can help. |
Ups, I was a little too shallow on my explanation, my bad... Reading and writing the file is not a problem. I'm working with Node {
"name": "my-library",
"version": "0.0.0",
"description": "awesome library", // Not interested
"dependencies": {
"react": "^18.0.0",
},
"devDependencies": {
"vite": "^4.0.0"
}
"husky": { // Also not interested
...
}
... // Other properties I don't need
} With marshmallow, I could have a struct defining the parts I want to read and modify: type PackageJSON struct {
Name string
Version string
Dependencies map[string]string
DevDependencies map[string]string
} The question is, how do I save the file, keeping also the "extra" I don't need in my struct but is needed in the final file |
Sure! You can. Marshmallow will allow you to work with a dataAsStruct := PackageJSON{}
dataAsMap, _ := marshmallow.Unmarshal(/* input from file */, &dataAsStruct)
// do stuff with dataAsStruct
out, _ := json.MarshalIndent(dataAsMap, "", "\t")
// write out to file It will work as expected for values that are pointers. When you change pointer values in the struct, it will also reflect the values in the map which you eventually write out. Note that maps are effectively pointers so if you plan to change
|
@MrMarble when you're available, please let us know if it was helpful or maybe there's something actionable here 🙏 |
Sorry I was busy at work today. That's just what I needed! Thank you for your time! Thank you! (feel free to close the issue) |
@MrMarble updated the docs accordingly. Thanks for the input. |
Hi!
I am looking for a way to read, modify and save a JSON of which I am only interested in part of its content.
This package looks like it could be useful for the first two steps, but I'm not sure about the last one.
Is there any way to do it? Either through the API of the package or in some recommended way.
Thanks!
(P.S. I tried to open a discussion instead of an issue but I can't do it)
The text was updated successfully, but these errors were encountered: