{"record":{"id":"6fc42c2b65664e86","repo":"mikefarah/yq","slug":"csv-object-encoding-only-works-for-arrays-of-flat-6fc42c","errorCode":null,"errorMessage":"csv object encoding only works for arrays of flat objects (string key => string/numbers/boolean value), child[%v] is a %v","messagePattern":"csv object encoding only works for arrays of flat objects \\(string key => string/numbers/boolean value\\), child\\[(.+?)\\] is a (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/encoder_csv.go","lineNumber":93,"sourceCode":"\t}\n\treturn childRow\n\n}\n\nfunc (e *csvEncoder) encodeObjects(csvWriter *csv.Writer, content []*CandidateNode) error {\n\theaders, err := e.extractHeader(content[0])\n\tif err != nil {\n\t\treturn nil\n\t}\n\n\terr = e.encodeRow(csvWriter, headers)\n\tif err != nil {\n\t\treturn nil\n\t}\n\n\tfor i, child := range content {\n\t\tif child.Kind != MappingNode {\n\t\t\treturn fmt.Errorf(\"csv object encoding only works for arrays of flat objects (string key => string/numbers/boolean value), child[%v] is a %v\", i, child.Tag)\n\t\t}\n\t\trow := e.createChildRow(child, headers)\n\t\terr = e.encodeRow(csvWriter, row)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t}\n\treturn nil\n}\n\nfunc (e *csvEncoder) Encode(writer io.Writer, node *CandidateNode) error {\n\tif node.Kind == ScalarNode {\n\t\treturn writeString(writer, node.Value+\"\\n\")\n\t}\n\n\tcsvWriter := csv.NewWriter(writer)\n\tcsvWriter.Comma = e.separator","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/encoder_csv.go#L75-L111","documentation":"In CSV object-encoding mode every element of the root array must be a flat mapping so its values can be placed under the header columns. This error fires when a non-first child of the array is not a mapping (after the header was successfully extracted from child[0]).","triggerScenarios":"`yq -o csv '[{a: 1}, \"oops\"]'` — child[0] yields the header, then encodeObjects hits child[1] which is a scalar, raising this error with child[1] is a !!str.","commonSituations":"Arrays with mixed element types (objects plus scalars/nulls); JSON data where some array entries are null; results of expressions like `.[] | select(...)` that emit heterogeneous items.","solutions":["Filter to only object elements: `yq '[.[] | select(type == \"!!map\")]'` before `-o csv`","Wrap scalars into objects: `map(select(. != null))` or `map({value: .})`","Normalize the data shape upstream so all array entries are flat objects"],"exampleFix":"// before: yq -o csv '[{a:1}, null]'\n// after:  yq -o csv '[.[] | select(type == \"!!map\")]' on input containing only objects","handlingStrategy":"validation","validationCode":"yq '[.[] | select(type != \"!!map\")] | length == 0' input.yaml  # must be true for object-CSV","typeGuard":"func allElementsAreMaps(node *CandidateNode) bool {\n  if node.Kind != SequenceNode { return false }\n  for _, c := range node.Content {\n    if c.Kind != MappingNode { return false }\n  }\n  return true\n}","tryCatchPattern":"if err := enc.Encode(node); err != nil {\n  if strings.Contains(err.Error(), \"child[\") {\n    // filter non-map children then retry\n  }\n}","preventionTips":["Filter nulls/scalars: `map(select(type == \"!!map\"))` before `-o csv`","Check `[.[] | type] | unique == [\"!!map\"]` prior to encoding","Keep source arrays homogeneous"],"tags":["csv","encoding","objects","mixed-types","yq"],"backgroundTag":"csv-nested-structure-unsupported","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"}