Saving json object to flat file

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.

For the record I’ve put your code into a play so we can reproduce your problem: https://play.golang.com/p/fGDUckeIPvf

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

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.

That is easily done: https://play.golang.com/p/kZyiphK6ve6

Please take a look at the documentation of the encoding/json package which explains all this.

1 Like

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 ?

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

Only playing around with these will give a better understanding. Thanks for helping me understand.

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/

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.