There's millions of lines each containing up to ~50 json dicts over which I'd like to iterate and store them in results map/struct. I can parse a line, but then do not know how to iterate over each line and store results. I'd like to get sth like this:
results["Turbine1"] containig params: status, sample, quality, time, inlet,...
results["Turbine2"] containig params: status, sample, quality, time, inlet,...
results["Turbine3"] containig params: status, sample, quality, time, inlet,...
results["Pump2"] containig params: status, sample, quality, time, intle,...
.
.
I was playing a lot with encoding/json.Unmarshal and var results []map[string]interface{} but got completely stuck at this point without success. Any help on how to iterate over single line, parse json dicts and store them in appropriate map/struct is greatly appreciated. Here is a minimal working example I came up with so far:
package main
import (
"bufio"
// "encoding/json"
"fmt"
"os"
"github.com/valyala/fastjson"
)
func main() {
f, _ := os.Open("measurements.log")
scanner := bufio.NewScanner(f)
// var results []map[string]interface{}
var p fastjson.Parser
for scanner.Scan() {
line := scanner.Text()
v, _ := p.Parse(line) // fastjson.Parse(line)
fmt.Println(v)
fmt.Printf("v[0] = %q\n", v.GetStringBytes("0", "ev"))
// json.Unmarshal(v, &results)
fmt.Println()
}
}
Im new to golang migrating from python. Am trying to convert a code from .py to .go. I have such a json file (
measurements.log
) with measurements:There's millions of lines each containing up to ~50 json dicts over which I'd like to iterate and store them in results map/struct. I can parse a line, but then do not know how to iterate over each line and store results. I'd like to get sth like this:
I was playing a lot with
encoding/json.Unmarshal
andvar results []map[string]interface{}
but got completely stuck at this point without success. Any help on how to iterate over single line, parse json dicts and store them in appropriate map/struct is greatly appreciated. Here is a minimal working example I came up with so far: