parsinggonedb

parse NeDB file in golang


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?


Solution

  • 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