{"record":{"id":"a2fa86edb25bebf1","repo":"mikefarah/yq","slug":"can-t-serialise-value-because-it-contains-nul-char","errorCode":null,"errorMessage":"can't serialise value because it contains NUL char and you are using NUL separated output","messagePattern":"can't serialise value because it contains NUL char and you are using NUL separated output","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/printer.go","lineNumber":134,"sourceCode":"\t\tvar destination io.Writer = writer\n\t\ttempBuffer := bytes.NewBuffer(nil)\n\t\tif p.nulSepOutput {\n\t\t\tdestination = tempBuffer\n\t\t}\n\n\t\tif err := p.encoder.PrintLeadingContent(destination, mappedDoc.LeadingContent); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tif err := p.printNode(mappedDoc, destination); err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tif p.nulSepOutput {\n\t\t\tremoveLastEOL(tempBuffer)\n\t\t\ttempBufferBytes := tempBuffer.Bytes()\n\t\t\tif bytes.IndexByte(tempBufferBytes, 0) != -1 {\n\t\t\t\treturn fmt.Errorf(\n\t\t\t\t\t\"can't serialise value because it contains NUL char and you are using NUL separated output\",\n\t\t\t\t)\n\t\t\t}\n\t\t\tif _, err := writer.Write(tempBufferBytes); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tif _, err := writer.Write([]byte{0}); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\n\t\tp.previousDocIndex = mappedDoc.GetDocument()\n\t\tif err := writer.Flush(); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tlog.Debugf(\"done printing results\")\n\t}\n","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/printer.go#L116-L152","documentation":"When NUL-separated output (`-0`/`--nul-separated-output`) is enabled, yq scans each serialized result for NUL bytes because NUL is the record delimiter. A value containing a NUL character would corrupt the output stream, so printing is aborted with this error.","triggerScenarios":"Outputting results with `-0` where a string value in the document contains an embedded NUL byte (\\0), e.g. binary-ish data or a literal null byte in a YAML string.","commonSituations":"Processing documents with binary blobs or control characters while using NUL separation for shell `xargs -0` pipelines; reading corrupted or binary files misidentified as YAML/JSON.","solutions":["Remove or sanitize the NUL character from the data, e.g. `yq '.[] |= gsub(\"\\\\u0000\"; \"\")'`","Switch to default (newline-separated) output and drop the `-0` flag if NUL-safe piping isn't required","Pre-clean the input file (e.g. `tr -d '\\0' < input.yaml`) before passing to yq","If binary data is expected, base64-encode the field first, e.g. `@base64`"],"exampleFix":"// before\nyq -0 '.[].blob' file.yaml\n// after\nyq '.[].blob | @base64' file.yaml\n","handlingStrategy":"validation","validationCode":"// Check values for NUL bytes before NUL-separated output\nif bytes.IndexByte(serialized, 0) != -1 {\n\treturn fmt.Errorf(\"value contains NUL; cannot use -0 output\")\n}","typeGuard":"func hasNul(s string) bool { return strings.ContainsRune(s, '\\x00') }","tryCatchPattern":"err := printer.PrintResults(...)\nif err != nil && strings.Contains(err.Error(), \"NUL char\") {\n\t// sanitize input and retry, or switch to newline-separated output\n}","preventionTips":["Sanitize or strip control characters from input documents","Avoid -0 output when handling potentially binary data","Encode binary fields (e.g. base64) before serialization","Validate input files are text, not binary"],"tags":["output","nul-separated","serialization"],"backgroundTag":"nul-character-in-output","analyzedSha":"8b5af0694bb82b41d4ae180fac9972029066f90a","analyzedAt":"2026-09-05T10:57:22.766Z","contentChangedAt":"2026-09-05T10:57:22.766Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}