Last active
March 24, 2021 05:40
-
-
Save polynomialspace/6d7a27fffa140883d1f3cfea91b905a8 to your computer and use it in GitHub Desktop.
ndjson -> json array (technically golang will take '\n' as a json sep where ndjson is '\r\n' but /shrug)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NDjsonTojsonArr converts newline-delimited json (indented or minified) | |
// to a json array. Caller should handle cleanup on error. | |
func NDjsonTojsonArr(r io.Reader, w io.WriteCloser) error { | |
w.Write([]byte("[")) | |
var m json.RawMessage | |
dec := json.NewDecoder(r) | |
err := dec.Decode(&m) | |
for { | |
if _, err = w.Write(m); err != nil { | |
return err | |
} | |
if err = dec.Decode(&m); err != nil { | |
if err != io.EOF { | |
return err | |
} | |
break | |
} | |
if _, err = w.Write([]byte(",")); err != nil { | |
return err | |
} | |
} | |
_, err = w.Write([]byte("]")) | |
return err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment