Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def beautify_response(response):
- new_response = {'users': []}
- specialties = {}
- for specialty in response['specialties']:
- specialties[specialty['id']] = {'id': specialty['id'], 'title':specialty['title']}
- for user in response['users']:
- new_response['users'].append({})
- new_response['users'][-1]['id'] = user.get('id')
- new_response['users'][-1]['surname'] = user.get('surname')
- new_response['users'][-1]['avatar_id'] = user.get('avatar_id')
- new_response['users'][-1]['specialty_ids'] = []
- for specialty in user.get('specialty_ids'):
- new_response['users'][-1]['specialty_ids'].append({})
- new_response['users'][-1]['specialty_ids'][-1]['id'] = specialties[specialty]['id']
- new_response['users'][-1]['specialty_ids'][-1]['title'] = specialties[specialty]['title']
- return new_response
- response = {
- 'users': [
- {
- 'id': 129,
- 'surname': 'Петров',
- 'specialty_ids': [
- 63
- ],
- 'avatar_id': None
- }
- ],
- 'specialties': [
- {
- 'id': 63,
- 'title': 'Программист'
- }
- ]
- }
- print(beautify_response(response))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement