On Sat, Jul 25, 2020 at 6:09 AM Amnon <amno...@gmail.com> wrote: > > Hi, > > I need to consume a stream of json objects. > But unfortunately the json objects are separated by commas. > > Is there any simple way I can convince the json.Decorder to skip the comma > after each successful call to decode?
You have to read the comma yourself from the stream after each element. You can read the next byte manually and discard it if it is a comma. If there can be other characters, you can wrap the reader with a bufio.Reader and use Read/Unread the comma/spaces between each JSON object. There is this package I wrote a while ago that does this, you can use that or get some ideas from it: https://github.com/bserdar/jsonstream > > My code looks like > > for { > var a st > err := dec.Decode(&a) > if err == io.EOF { > break > } > if err != nil { > log.Fatalln(err) > } > log.Println(a) > } > > > https://play.golang.org/p/7lBhs0pXUx8 > > Unfortunately the json comes from an external service, so I can't stop it > inserting unecessesary comments. > > Thanks for any ideas, > Amnon > > -- > 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. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/e2a50435-aacd-4c13-8c79-2ce1f8dd3f8fo%40googlegroups.com. -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAMV2RqrTq0MoAQZE%2BM2YeSpK9qNCWM2QMrpHg3fqrDCrH4GuoQ%40mail.gmail.com.