[go-nuts] Re: non-standard json
On Thursday, November 1, 2018 at 5:14:17 PM UTC+1, Alex Dvoretskiy wrote: > > Is there is a way to read a file line by line and extract JSON data from > it? > > > Example: > > file: > ` > value1: {"field1": "123, "field2": "123"} > value2: {"field1": "456", "field2": "879"} > > ` > > Of course. Use https://golang.org/pkg/bufio/#Scanner to parse each line. Then use https://golang.org/pkg/strings/#SplitN `Split(line, ":", 2)` to split the key and the value (`key: value`). Finally parse the value using https://golang.org/pkg/encoding/json/#Unmarshal. Not tested. > [...] Manlio -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Re: non-standard json
Try something like https://play.golang.org/p/eHEjKINz9aW func extract(r io.Reader) { dec := json.NewDecoder(r) for { var el s err := dec.Decode() if err == io.EOF { break } log.Println(el) } } On Thursday, 1 November 2018 16:14:17 UTC, Alex Dvoretskiy wrote: > > Is there is a way to read a file line by line and extract JSON data from > it? > > > Example: > > file: > ` > value1: {"field1": "123, "field2": "123"} > value2: {"field1": "456", "field2": "879"} > > ` > > I need to extract the struct in a loop: > > type s struct { > field1 string > field2 string > } > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[go-nuts] Re: non-standard json
If the file is consistent parse the Json part out of each line and just unmarshal it. On Thursday, 1 November 2018 16:14:17 UTC, Alex Dvoretskiy wrote: > > Is there is a way to read a file line by line and extract JSON data from > it? > > > Example: > > file: > ` > value1: {"field1": "123, "field2": "123"} > value2: {"field1": "456", "field2": "879"} > > ` > > I need to extract the struct in a loop: > > type s struct { > field1 string > field2 string > } > > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.