我有一个JSON对象,它看起来像
{
"timestamp": "2020-06-24T15:32:56.775518Z",
"record-type": "data",
"operation": "load",
"partition-key-type": "primary-key",
"schema-name": "legacy",
"table-name": "test"
}我试着把这些记录反序列化成
class MetadataModel(faust.Record):
timestamp: str
record-type: str # does not work, hyphens in name not allowed
schema_name: str # does not work
tableName: str # does not work 我可能遗漏了一些简单的东西,但是如何从键中有连字符的json对象转到python对象呢?任何帮助都将不胜感激!
发布于 2021-10-05 18:03:56
您可以使用faust.models.fields中的字段类在JSON中提供自定义名称。
import faust
from faust.models.fields import StringField
class MetadataModel(faust.Record):
timestamp: str
record_type: str = StringField(input_name='record-type')
operation: str
partition_key_type: str = StringField(input_name='partition-key-type')
schema_name: str = StringField(input_name='schema-name')
table_name: str = StringField(input_name='table-name')
json_bytes = b"""
{
"timestamp": "2020-06-24T15:32:56.775518Z",
"record-type": "data",
"operation": "load",
"partition-key-type": "primary-key",
"schema-name": "legacy",
"table-name": "test"
}
"""
print(MetadataModel.loads(json_bytes, default_serializer='json'))输出:
<MetadataModel: timestamp='2020-06-24T15:32:56.775518Z', record_type='data', operation='load', partition_key_type='primary-key', schema_name='legacy', table_name='test'>https://stackoverflow.com/questions/62560484
复制相似问题