Yo_94
(Hamxa)
June 2, 2020, 11:36pm
1
Hey,
I am converting data into the JSON format, and I am trying to write that data into the file but data is not writing into the file as expected.
For example:
I am using
DetailJson, err := json.MarshalIndent(Data, "", "\t")
outpuFile := os.create(File)
outPutfile.Write(*DetailJson)
This works fine but Data insert on the text file as
{“ID”:12345}
{“ID”:45678}
but I want in the following format, can someone please help me with this?
[
{“ID”:12345},
{“ID”:45678},
]
NobbZ
(Norbert Melzer)
June 3, 2020, 5:19am
2
What is the type of your Data
and its value?
1 Like
Yo_94
(Hamxa)
June 3, 2020, 8:30am
3
It’s a struct which has one field ID and type String
NobbZ
(Norbert Melzer)
June 3, 2020, 8:36am
4
A single struct will not be rendered as JSON list. Though a single struct will also not be rendered as two JSON objects, so you are hiding some relevant parts of the code.
Yo_94
(Hamxa)
June 3, 2020, 12:58pm
5
I am reading data from the database, so I want each record to be save as the new JSON object
ermanimer
(erman imer)
June 3, 2020, 1:21pm
6
You see square brackets [ ] in the output beacuse your are marshall indenting a slice or an array (of objects).