shwink
(Ashwin K)
March 1, 2018, 10:55am
1
I have a requirement to save the json and later modified fields of structs after unmarshalling ,to be written back to the same file.
Suppose I have structs like below
type struct1 struct{
St1B bool
St1I int
}
type struct2 struct{
St2S string
St2B bool
}
I initialize the values
pp := &struct1{
St1B : true,
St1I : 23,
}
dd := &struct2{
St2S : "Hello",
St2B : false,
}
After inserting it into a map like below
Configs = make(map[string]interface{})
Configs["st1"] = pp
Configs["st2"] = dd
How would I go about writing it a json file and read it so as to it unmarshalls to the right structure ?
I tried doing the below,but it would only put “pp” struct, not other structs like “dd” or others if available.
jdata1, err := json.MarshalIndent(pp, "", " ")
if err != nil {
fmt.Println("error:", err)
}
fmt.Println(string(jdata1))
jsonFile, err := os.Create("./data.json")
jsonFile.Write(jdata1)
Basically I’m trying to save this struct values to a flat file and be able to retrieve it before modifying it.
Any suggestions appreciated.
lutzhorn
(Lutz Horn)
March 1, 2018, 10:58am
2
For the record I’ve put your code into a play so we can reproduce your problem: https://play.golang.com/p/fGDUckeIPvf
lutzhorn
(Lutz Horn)
March 1, 2018, 10:59am
3
Just marshal Configs
, not only pp
:
jdata1, err := json.MarshalIndent(Configs, "", " ")
Result:
{
"st1": {
"St1B": true,
"St1I": 23
},
"st2": {
"St2S": "Hello",
"St2B": false
}
}
see https://play.golang.com/p/v_66tYcLAlI
1 Like
shwink
(Ashwin K)
March 1, 2018, 11:09am
4
Thanks Iutzhorn , that was pretty easily done. Beign a newbie to this stuff ,I would now attempt to read from a json file and unmarshall into the two different structs.It would be great if can suggest on how to go about doing that.
lutzhorn
(Lutz Horn)
March 1, 2018, 11:17am
5
1 Like
shwink
(Ashwin K)
March 2, 2018, 8:41am
6
Ok, I may be wrong but , what I was trying to achieve is : https://play.golang.com/p/g3clTg9pfiA
configs2 := struct2{}
err2 := json.Unmarshal([]byte(jsonBlob2), &configs2)
expecting ‘configs2’ to be populated with the unmarshalled value of the "struct2"
Here is currently the result is
%q { false}
which is the default value of bool inside struct2
Why doesn’t it unmarshall into the struct directly ?
lutzhorn
(Lutz Horn)
March 2, 2018, 8:48am
7
Because you try to unmarshall this JSON
{
"st2": {
"St2S": "Hello",
"St2B": true
}
}
into this:
type struct2 struct {
St2S string
St2B bool
}
You will need an inner, exported member of struct2
that can receive st2
:
package main
import (
"encoding/json"
"fmt"
)
type struct2 struct {
St2 struct2inner
}
type struct2inner struct {
St2S string
St2B bool
}
func main() {
jsonBlob2 := `{
"st2": {
"St2S": "Hello",
"St2B": true
}
}`
configs2 := struct2{}
err2 := json.Unmarshal([]byte(jsonBlob2), &configs2)
if err2 != nil {
fmt.Println("error:", err2)
}
fmt.Println("%q", configs2)
}
Result:
%q {{Hello true}}
see https://play.golang.com/p/poNx2kFsg0p
2 Likes
shwink
(Ashwin K)
March 2, 2018, 9:16am
8
Only playing around with these will give a better understanding. Thanks for helping me understand.
coleman
(Coleman McFarland)
March 7, 2018, 4:24am
9
This tool from mholt gives you a way to see how json objects map to Go struct types. https://mholt.github.io/json-to-go/
system
(system)
Closed
June 5, 2018, 4:24am
10
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.