3

everyone! I'm new in programming, so, please, be lenient :) I have a .db file from NeDB that looks like json:

{"key":"User","value":{"user":{"userId":"13","name":"Test","lastname":"Test","email":"test@test.com"},"token":"ELMZZR38kxPkdjnSttZOfM0F5iDo3t4eMVjCNH0"}}
{"key":"Words","value":"flight syrup high actor reason","_id":"MvSx29","createdAt":{"$$date":1592210725916},"updatedAt":{"$$date":1592210725916}}
{"key":"StartDate","value":{"$$date":1594039122453},"_id":"TqYA66Rd","createdAt":{"$$date":1594039122484},"updatedAt":{"$$date":1594039122484}}

I tried to parse it like json, but it didn't work...
How can I parse it to get specific values (like userId, words) and put it to json structure?

rs256
  • 69
  • 6
  • 1
    I wrote this go package a while ago to deal with this kind of input: https://github.com/bserdar/jsonstream It uses the standard library decoder. You can see the code to see how it is done or use the package itself. – Burak Serdar Jul 06 '20 at 15:30
  • 2
    Does this answer your question? [Parsing multiple JSON objects in Go](https://stackoverflow.com/questions/50350010/parsing-multiple-json-objects-in-go) – Peter Jul 06 '20 at 16:24

1 Answers1

4

It looks like JSON with a document per line; you may be able to parse it using encoding/json.Decoder, which allows for stream parsing. Pass it your reader and then just keep calling Decode, you should get one object (row) per call:

dc := json.NewDecoder(bytes.NewReader(corpus))
var obj map[string]interface{}
var err error
for err = dc.Decode(&obj); err == nil; err = dc.Decode(&obj) {
    fmt.Println(obj)  // Or pull whatever fields you need
}

https://play.golang.org/p/CV4Fx31J5-k

Adrian
  • 42,911
  • 6
  • 107
  • 99