#Help with writing records to JSON

15 messages · Page 1 of 1 (latest)

glad narwhal
#

My JSON structure will be as follows -

[
  "string": {
    //Data obtained from GO Struct, contains various strings and ints  
  },
  "string2": {
    // Data obtained from GO Struct, contains various strings and int
  }
]

Is there a way to store a struct as a JSON and more importantly, can I write records (elements in main array) one by one?
Like how you can write a line to a CSV file.

inland steppe
#

you cant, a JSON array cannot have a key

#

only objects can have a key

#

normally to maintain ordered arrays you can do

#
[
{"key":"foobar", "data":"..."},
{"key":"baroof", "data":"..."}
]
glad narwhal
#

oh sorry i made a typo there

#

however, can you write singular records?

inland steppe
#

Like how you can write a line to a CSV file.
sorry seems like i missed that, but you cannot do that with conventional means

#

maybe there's a library that could allow you to flush json in chunks but i not with standard lib

glad narwhal
#

gotcha, also another question extending on that one.
I want to execute some code after a crash or after user stops execution, is this possible?
basically something that will write my records to a file if user decides to cancel mid process

inland steppe
#

check out defer recover
do note that you cant necessary guarantee it will happen, if say user forcefully killed your app

glad narwhal
#

oh-

#

so a control+c in the terminal counts as...?

inland steppe
#

i think that's an interupt