{"record":{"id":"81186d2ccc227ca0","repo":"apache/beam","slug":"cannot-convert-s-to-a-json-value","errorCode":null,"errorMessage":"Cannot convert %s to a JSON value.","messagePattern":"Cannot convert (.+?) to a JSON value\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/internal/gcp/json_value.py","lineNumber":122,"sourceCode":"  elif isinstance(obj, str):\n    return extra_types.JsonValue(string_value=obj)\n  elif isinstance(obj, bytes):\n    return extra_types.JsonValue(string_value=obj.decode('utf8'))\n  elif isinstance(obj, bool):\n    return extra_types.JsonValue(boolean_value=obj)\n  elif isinstance(obj, int):\n    if _MININT64 <= obj <= _MAXINT64:\n      return extra_types.JsonValue(integer_value=obj)\n    else:\n      raise TypeError('Can not encode {} as a 64-bit integer'.format(obj))\n  elif isinstance(obj, float):\n    return extra_types.JsonValue(double_value=obj)\n  elif isinstance(obj, ValueProvider):\n    if obj.is_accessible():\n      return to_json_value(obj.get())\n    return extra_types.JsonValue(is_null=True)\n  else:\n    raise TypeError('Cannot convert %s to a JSON value.' % repr(obj))\n\n\ndef from_json_value(v):\n  \"\"\"For internal use only; no backwards-compatibility guarantees.\n\n  Converts ``extra_types.JsonValue`` objects into Python objects.\n\n  Args:\n    v: ``JsonValue`` object to be converted.\n\n  Returns:\n    A Python object structured as values, lists, and dictionaries corresponding\n    to ``JsonValue``, ``JsonArray`` and ``JsonObject`` types.\n\n  Raises:\n    TypeError: if the ``JsonValue`` object contains a type that is\n      not supported.\n","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/internal/gcp/json_value.py#L104-L140","documentation":"apache_beam.internal.gcp.json_value.to_json_value converts Python objects (str, bytes, int, float, bool, dict, list, proto types, ValueProviders) into extra_types.JsonValue protobuf wrappers. When the input object is none of the supported types, the function raises TypeError with the repr of the offending object. It exists because JsonValue is a oneof-wrapped protobuf and cannot hold arbitrary Python objects.","triggerScenarios":"Calling to_json_value(obj) where obj is not str, bytes, bool, int, float, None, dict/list of convertible values, a recognized protobuf type (StructuredValue, Row, LogicalType), or an accessible ValueProvider — e.g. passing a set, datetime, numpy scalar, or custom class.","commonSituations":"Users pass datetime.date/numpy.int64 values from pipelines into BigQuery helper paths that serialize via JsonValue; passing an inaccessible or exotic object type; internal Beam code receiving unexpectedly typed config values.","solutions":["Convert the object to a supported type first (str, int, float, bool, dict, list, or None) before calling to_json_value","For datetime objects, serialize explicitly with .isoformat() or .timestamp()","For numpy scalars, cast with .item() or int()/float()","If passing a ValueProvider, ensure it is accessible (is_accessible() True) or accept the null default","Wrap the call in try/except TypeError and provide a custom serialization for your type"],"exampleFix":"// before\nfrom apache_beam.internal.gcp.json_value import to_json_value\nimport datetime\nv = to_json_value(datetime.date(2024, 1, 1))  # TypeError\n// after\nv = to_json_value(datetime.date(2024, 1, 1).isoformat())","handlingStrategy":"validation","validationCode":"def is_json_convertible(obj):\n    return obj is None or isinstance(obj, (str, bytes, bool, int, float, dict, list))","typeGuard":"def is_json_convertible(obj):\n    return obj is None or isinstance(obj, (str, bytes, bool, int, float, dict, list))","tryCatchPattern":"try:\n    jv = to_json_value(obj)\nexcept TypeError as e:\n    jv = to_json_value(str(obj))  # or custom serializer","preventionTips":["Restrict inputs to JSON-native types (str, int, float, bool, None, list, dict)","Convert datetime/numpy/custom objects explicitly before serialization","Unit-test serialization paths with your real element types"],"tags":["python","type-error","serialization","json","google-cloud"],"backgroundTag":"json-serialization-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}