A custom property transformer to translate a ndb.JsonProperty into a repeated record with fields for each of the keys in the original JSON.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TransformedVideoTranslationInfo(bq_property_transform.TransformedEntity): | |
CUSTOM_SCHEMAS = { | |
'translated_youtube_ids': { | |
'name': 'translated_youtube_ids', | |
'type': 'record', | |
'mode': 'repeated', | |
'fields': [ | |
{'name': 'language', | |
'type': 'string'}, | |
{'name': 'youtube_id', | |
'type': 'string'} | |
] | |
} | |
} | |
@classmethod | |
def transform_custom_property(cls, entity, schema_field): | |
"""Do a custom transform for the translated_youtube_ids field. | |
This will convert the json property to a repeated record property | |
containing subproperties 'language' and 'youtube_id' for each | |
translation. | |
""" | |
if schema_field['name'] == 'translated_youtube_ids': | |
languages = [] | |
parsed_json = json.loads(entity[schema_field['name']]) | |
for k, v in parsed_json.items(): | |
languages.append({'language': k, 'youtube_id': v}) | |
return {'translated_youtube_ids': languages} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment