I have a question regarding the parsing of a json array. In the case below I have several assessment id's that have a topic. These in turn have questions and these in turn have answers.
My goal is to parse the json array to the following format:
{
assessment_id: 1
topics[
{topic_id: 1},
{...}
questions[
{question_id: 1},
{...}
answers[
{answer_id: 1}
]
]
]
}
I already used itertools, but I can't seem to correctly parse the sub-arrays such as topics, questions and answers.
Below the format gathered by the database can be found:
[{'assessment_id': 1, 'region': '', 'status': True, 'language': 'ENG', 'progress': 'ok', 'urls': None, 'overall_score': None, 'wwid': 1, 'name': '', 'topic_id': 1, 'answer_option_id': 1, 'question_text': '', 'answer': '', 'image_url': None, 'topic': '', 'question_id': 1, 'lang_code': '', 'help': None, 'info': None, 'answer_scores': ''},
{'assessment_id': 2, 'region': '', 'status': False, 'language': '', 'progress': None, 'urls': None, 'overall_score': None, 'wwid': 1, 'name': '', 'topic_id': 1, 'answer_option_id': 1, 'question_text': '', 'answer': '', 'image_url': None, 'topic': '', 'question_id': 1, 'lang_code': '', 'help': None, '': None, 'answer_score': '' }]
Below is the code that I have right now, with the output at the moment:
res = []
key_func = lambda x:(x['assessment_id'])
for k, a in itertools.groupby(sorted(values, key=key_func),
key=key_func):
obj = {
'region': '',
'questionnaire_id': k,
'region': '',
'status': '',
'language': '',
'progress': '',
'wwid': '',
'name': '',
'urls': [],
'overall_score': '',
'questions': [],
# 'topic_ids': [],
# 'answer_option_ids': [],
'topics': [],
}
for ids in a:
if not obj['region']:
obj['region'] = ids['region']
if not obj['questionnaire_id']:
obj['questionnaire_id'] = ids['id']
if not obj['status']:
obj['status'] = ids['status']
if not obj['language']:
obj['language'] = ids['language']
if not obj['progress']:
obj['progress'] = ids['progress']
if not obj['wwid']:
obj['wwid'] = ids['wwid']
if not obj['name']:
obj['name'] = ids['name']
if not obj['overall_score']:
obj['overall_score'] = ids['overall_score']
# obj['topic_ids'].append(ids['topic_id'])
obj['urls'].append(ids['urls'])
obj['topics'].append({
'topic_id': ids['topic_id'],
'topic': ids['topic'],
})
key_func = lambda x:(x['question_id'])
for j, z in itertools.groupby(sorted(a, key=key_func),
key=key_func):
obj['questions'].append({
'question_id': j
})
obj['topics'].append(obj['questions'])
res.append(obj)
print(res)
output:
[{'region': 'AUS', 'questionnaire_id': 1, 'status': True, 'language': 'ENG', 'progress': 'ok', 'wwid': 1, 'name': 'Kjell', 'urls': [None], 'overall_score': None, 'questions': [{'question_id': 1}, {'question_id': 2}], 'topics': [{'topic_id': 1, 'topic': 'AW'}, [{'question_id': 1}, {'question_id': 2}]]}, {'region': 'AUS',
'questionnaire_id': 2, 'status': False, 'language': 'ENG', 'progress': None, 'wwid': 1, 'name': 'Kjell', 'urls': [None], 'overall_score': None, 'questions': [{'question_id': 1}], 'topics': [{'topic_id': 1, 'topic': 'AW'}, [{'question_id': 1}]]}]
from Order JSON array by keys
No comments:
Post a Comment