1

I have a JSON object that looks like

{ 
  "timestamp": "2020-06-24T15:32:56.775518Z",
  "record-type": "data",
  "operation": "load",
  "partition-key-type": "primary-key",
  "schema-name": "legacy",
  "table-name": "test"
}

and I'm trying to deserialize the records into

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 

I'm probably missing something simple, but how do I go from a json object that has hyphenations in the keys to a python object. Any help would be much appreciated!

Matthias J. Sax
  • 59,682
  • 7
  • 117
  • 137
qbzenker
  • 4,412
  • 3
  • 16
  • 23
  • Maybe provide the code for serialization / deserialization that you are using. Are you using json.dumps() or MetadataModel.dumps(). A short functional example if possible would also be helpful – Valerian Jan 25 '21 at 12:34

1 Answers1

2

You can use field classes from faust.models.fields to provide custom names in 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'))

Output:

<MetadataModel: timestamp='2020-06-24T15:32:56.775518Z', record_type='data', operation='load', partition_key_type='primary-key', schema_name='legacy', table_name='test'>
Aidar
  • 106
  • 1
  • 7