{"record":{"id":"39139d10e804bb07","repo":"pola-rs/polars","slug":"expected-list-or-dict-of-objects","errorCode":null,"errorMessage":"expected list or dict of objects","messagePattern":"expected list or dict of objects","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/convert/normalize.py","lineNumber":246,"sourceCode":"    ╞══════╪════════════╪═══════════════════════════════╡\n    │ 1    ┆ Cole Volk  ┆ b\"{\"height\":180,\"weight\":85}\" │\n    │ 2    ┆ Faye Raker ┆ b\"{\"height\":155,\"weight\":58}\" │\n    │ null ┆ Mark Reg   ┆ b\"{\"height\":170,\"weight\":78}\" │\n    └──────┴────────────┴───────────────────────────────┘\n    \"\"\"\n    if max_level is None:\n        max_level = 1 << 32  # eg: u32\n    max_level += 1\n\n    if isinstance(data, Sequence) and len(data) == 0:\n        return DataFrame(schema=schema)\n    elif isinstance(data, Mapping):\n        data = [data]\n    elif isinstance(data, Iterable) and not isinstance(data, str):  # type: ignore[redundant-expr]\n        data = list(data)\n    else:\n        msg = \"expected list or dict of objects\"\n        raise ValueError(msg)\n\n    if encoder is None:\n        encoder = json.dumps\n\n    return DataFrame(\n        _simple_json_normalize(\n            data,\n            separator=separator,\n            max_level=max_level,\n            encoder=encoder,\n        ),\n        schema=schema,\n        strict=strict,\n        infer_schema_length=infer_schema_length,\n    )\n","sourceCodeStart":228,"sourceCodeEnd":262,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/convert/normalize.py#L228-L262","documentation":"`pl.json_normalize` flattens semi-structured JSON records into a DataFrame. This ValueError is raised when `data` is neither a dict (single record), a Mapping, nor an Iterable of such objects (and not a string).","triggerScenarios":"Calling `pl.json_normalize(data)` with `data=None`, a scalar (int/str), a generator of non-dict items already consumed, or any non-iterable object.","commonSituations":"API call returned None or a scalar (error payload) instead of a list of records; accidentally passing a JSON string (use `pl.read_json` or `json.loads` first); passing a single dict wrapped wrong — single dicts are accepted, but scalars are not.","solutions":["Ensure `data` is a list of dicts or a single dict; wrap a single record as `[record]` only if it is a Mapping (a bare Mapping is already accepted).","Parse JSON strings first: `pl.json_normalize(json.loads(text))` or use `pl.read_json`.","Guard with `isinstance(data, (dict, list))` and an informative fallback before calling."],"exampleFix":"// before\npl.json_normalize(response_text)  # str\n// after\nimport json\npl.json_normalize(json.loads(response_text))","handlingStrategy":"validation","validationCode":"if data is None or isinstance(data, (str, int, float)):\n    raise ValueError('json_normalize needs a list of dicts or a single dict')","typeGuard":"def is_records(x) -> bool:\n    from collections.abc import Mapping\n    return isinstance(x, Mapping) or (isinstance(x, list) and all(isinstance(i, Mapping) for i in x))","tryCatchPattern":"try:\n    df = pl.json_normalize(data)\nexcept ValueError:\n    df = pl.DataFrame()  # or log and skip empty/invalid payload","preventionTips":["json.loads() strings before normalize; use read_json for raw JSON files","Validate API payloads are lists of objects before normalizing","Handle None/error responses upstream"],"tags":["python","polars","json","value-error"],"backgroundTag":"invalid-argument-value","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}