How to convert data into Json and save in a file with Foramt?


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)

This works fine but Data insert on the text file as

but I want in the following format, can someone please help me with this?


What is the type of your Data and its value?

1 Like

It’s a struct which has one field ID and type String

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.

I am reading data from the database, so I want each record to be save as the new JSON object

You see square brackets [ ] in the output beacuse your are marshall indenting a slice or an array (of objects).