{"record":{"id":"f59ca487497d8b5b","repo":"dgraph-io/dgraph","slug":"only-array-of-map-allowed-at-root","errorCode":null,"errorMessage":"only array of map allowed at root","messagePattern":"only array of map allowed at root","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"chunker/json_parser.go","lineNumber":731,"sourceCode":"\t\t\t}\n\t\t\tbuf.checkForDeletion(mr, m, op)\n\t\t} else if typ == simdjson.TypeArray {\n\t\t\t// the root element is an array, so parse the array\n\t\t\tif arr, err = tmp.Array(arr); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\t// attempt to convert to []interface{}\n\t\t\ta, err := arr.Interface()\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tif len(a) > 0 {\n\t\t\t\t// attempt to convert each array element to a\n\t\t\t\t// map[string]interface{} for further parsing\n\t\t\t\tvar o interface{}\n\t\t\t\tfor _, o = range a {\n\t\t\t\t\tif _, ok := o.(map[string]interface{}); !ok {\n\t\t\t\t\t\treturn errors.New(\"only array of map allowed at root\")\n\t\t\t\t\t}\n\t\t\t\t\t// pass to next parsing stage\n\t\t\t\t\tmr, err := buf.mapToNquads(o.(map[string]interface{}), op, \"\")\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn err\n\t\t\t\t\t}\n\t\t\t\t\tbuf.checkForDeletion(mr, o.(map[string]interface{}), op)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\tdefault:\n\t\treturn errors.New(\"initial element not found in json\")\n\t}\n\treturn nil\n}\n\n// ParseJSON parses the given byte slice and pushes the parsed NQuads into the buffer.\nfunc (buf *NQuadBuffer) ParseJSON(b []byte, op int) error {","sourceCodeStart":713,"sourceCodeEnd":749,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/chunker/json_parser.go#L713-L749","documentation":"FastParseJSON (chunker/json_parser.go:731) expects the parsed JSON root to be an array of objects; each array element must be a map[string]interface{} representing a node. If any element is not a map, the fast parser stops with this error. It exists because the streaming fast path cannot process non-object root entries.","triggerScenarios":"Calling FastParseJSON on a JSON array whose root contains non-object elements, e.g. [1,2,3] or [\"a\",\"b\"], or a scalar root like 42.","commonSituations":"Export files from other tools that emit arrays of scalars, or JSONL lines that are bare values rather than objects; users hitting the fast bulk-load path with malformed exports.","solutions":["Make the root a JSON array of objects: [{...},{...}].","Wrap bare scalar values into objects with a predicate key before parsing.","Use ParseJSON instead if your input format legitimately differs, or preprocess/convert the document."],"exampleFix":"// before\n[\"alice\", \"bob\"]\n// after\n[{\"name\": \"alice\"}, {\"name\": \"bob\"}]","handlingStrategy":"validation","validationCode":"func rootIsArrayOfMaps(b []byte) error {\n\tvar a []interface{}\n\tif err := json.Unmarshal(b, &a); err != nil {\n\t\treturn err\n\t}\n\tfor i, e := range a {\n\t\tif _, ok := e.(map[string]interface{}); !ok {\n\t\t\treturn fmt.Errorf(\"element %d is not an object\", i)\n\t\t}\n\t}\n\treturn nil\n}","typeGuard":"func isArrayOfMaps(v interface{}) bool {\n\ta, ok := v.([]interface{})\n\tif !ok {\n\t\treturn false\n\t}\n\tfor _, e := range a {\n\t\tif _, ok := e.(map[string]interface{}); !ok {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn len(a) > 0\n}","tryCatchPattern":"if err := buf.FastParseJSON(b, op); err != nil && strings.Contains(err.Error(), \"only array of map allowed at root\") {\n\t// wrap scalar entries in objects and retry\n}","preventionTips":["Always emit a root array of objects for bulk JSON imports","Validate the root shape with json.Unmarshal into []map[string]interface{} first","Convert scalar-only exports before feeding the fast parser"],"tags":["json-parser","input-validation","root-element","dgraph"],"backgroundTag":"invalid-json-root-structure","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}