Skip to content

Instantly share code, notes, and snippets.

@MattFaus
Created March 22, 2014 00:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattFaus/9699344 to your computer and use it in GitHub Desktop.
Save MattFaus/9699344 to your computer and use it in GitHub Desktop.
A custom property transformer to translate a ndb.JsonProperty into a repeated record with fields for each of the keys in the original JSON.
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