Оптимизация разбра JSON в Python
Представленный ниже код полностью рабочий и выполняет свои задачи.
Интересует возможность его оптимизации либо реализация без использования сторонних библиотек.
Заранее благодарю за конструктивные предложения
Код:
incoming.json
{
"questions": [
{
"key": "what_type_of_business_do_you_operate?",
"label": "What type of business do you operate?",
"options": [
{
"key": "reseller",
"value": "Reseller"
},
{
"key": "logistics_company",
"value": "Logistics company"
},
{
"key": "other",
"value": "Other"
}
],
"type": "CUSTOM",
"id": "****************"
},
{
"key": "conditional_question_1",
"label": "Please choose the truck brand your are interested in",
"options": [
{
"key": "volvo",
"value": "Volvo"
},
{
"key": "mercedes-benz_actros",
"value": "Mercedes-Benz Actros"
},
{
"key": "daf",
"value": "DAF"
}
],
"type": "CUSTOM",
"dependent_conditional_questions": [
{
"name": "Please choose age of the vehicle",
"input_type": "inline_select",
"field_key": "conditional_question_2"
},
{
"name": "Quantity you want to purchase",
"input_type": "inline_select",
"field_key": "conditional_question_3"
},
{
"name": "When do you plan to purchase",
"input_type": "inline_select",
"field_key": "conditional_question_4"
}
],
"conditional_questions_choices": [
{
"value": "Volvo",
"customized_token": "volvo",
"next_question_choices": [
{
"value": "1-3 years",
"customized_token": "1-3_years",
"next_question_choices": [
{
"value": "1",
"customized_token": "1",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
},
{
"value": "2",
"customized_token": "2",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
},
{
"value": "3-5",
"customized_token": "3-5",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
},
{
"value": "more than 5",
"customized_token": "more_than_5",
"next_question_choices": [
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
}
]
},
{
"value": "3-5 years",
"customized_token": "3-5_years",
"next_question_choices": [
{
"value": "1",
"customized_token": "1",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
},
{
"value": "2",
"customized_token": "2",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
},
{
"value": "3-5",
"customized_token": "3-5",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
},
{
"value": "more than 5",
"customized_token": "more_than_5",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
}
]
}
]
},
{
"value": "Mercedes-Benz Actros",
"customized_token": "mercedes-benz_actros",
"next_question_choices": [
{
"value": "1-3 years",
"customized_token": "1-3_years",
"next_question_choices": [
{
"value": "1",
"customized_token": "1",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
},
{
"value": "2",
"customized_token": "2",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
},
{
"value": "3-5",
"customized_token": "3-5",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
},
{
"value": "more than 5",
"customized_token": "more_than_5",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
}
]
},
{
"value": "3-5 years",
"customized_token": "3-5_years",
"next_question_choices": [
{
"value": "1",
"customized_token": "1",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
},
{
"value": "2",
"customized_token": "2",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
},
{
"value": "3-5",
"customized_token": "3-5",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
},
{
"value": "more than 5",
"customized_token": "more_than_5",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
}
]
}
]
},
{
"value": "DAF",
"customized_token": "daf",
"next_question_choices": [
{
"value": "3-5 years",
"customized_token": "3-5_years",
"next_question_choices": [
{
"value": "1",
"customized_token": "1",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
},
{
"value": "2",
"customized_token": "2",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
},
{
"value": "3-5",
"customized_token": "3-5",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
},
{
"value": "more than 5",
"customized_token": "more_than_5",
"next_question_choices": [
{
"value": "In upcoming days or weeks",
"customized_token": "in_upcoming_days_or_weeks"
},
{
"value": "Within 3 months",
"customized_token": "within_3_months"
},
{
"value": "This year",
"customized_token": "this_year"
},
{
"value": "Next year",
"customized_token": "next_year"
}
]
}
]
}
]
}
],
"conditional_questions_group_id": "1781381718904816",
"id": "****************"
},
{
"key": "company_name",
"label": "Company name",
"type": "COMPANY_NAME",
"id": "****************"
},
{
"key": "full_name",
"label": "Full name",
"type": "FULL_NAME",
"id": "****************"
},
{
"key": "phone_number",
"label": "Phone number",
"type": "PHONE",
"id": "****************"
},
{
"key": "email",
"label": "Email",
"type": "EMAIL",
"id": "****************"
}
],
"locale": "en_US",
"name": "Resellers - Retail lead form test 220908 multiple sales people",
"created_time": "2022-09-08T11:32:03+0000",
"context_card": {
"title": "Testing multiple sales people",
"content": [
"aekejskal;fjkldsfjdklsfj"
],
"style": "PARAGRAPH_STYLE",
"id": "****************"
},
"id": "****************"
}
Обработчик:
pip install jmespath
import jmespath as jp
import json
data: dict = json.load(
open('incoming.json', 'r', encoding='utf-8'),
)
form_questions = dict()
for item in jp.search(
'questions[?dependent_conditional_questions!=null]',
data
):
item: dict
questions = {
item.get('key'): {
'label': item.get('label'),
'type': 'CONDITIONAL'
}
}
for elem in jp.search(
'dependent_conditional_questions[*].[field_key,{label:name}]',
item
):
elem[1]['type'] = 'CONDITIONAL'
questions.update(
(elem,)
)
for i, key in enumerate(questions):
pat = f'conditional_questions_choices' + ('[0].next_question_choices' * i) + '[*].[customized_token,value]'
asd = questions[key]['values'] = dict(jp.search(pat, item))
form_questions.update(
questions
)
for item in jp.search(
"questions[?dependent_conditional_questions==null && type=='CUSTOM']",
data
):
item: dict
form_questions.update(
{
item.get('key'):
{
'label': item.get('label'),
'values': {
elem.get('key'): elem.get('value')
for elem in item.get('options', [])
},
'type': 'CUSTOM'
}
}
)
for item in jp.search(
"questions[?type=='COMPANY_NAME' || type=='PHONE' || type=='FULL_NAME' || type=='EMAIL']",
data
):
item: dict
form_questions.update(
{
item.get('key'): {
'label': item.get('label'),
'type': item.get('type'),
'values': dict()
}
}
)
Результат:
{
"conditional_question_1": {
"label": "Please choose the truck brand your are interested in",
"type": "CONDITIONAL",
"values": {
"volvo": "Volvo",
"mercedes-benz_actros": "Mercedes-Benz Actros",
"daf": "DAF"
}
},
"conditional_question_2": {
"label": "Please choose age of the vehicle",
"type": "CONDITIONAL",
"values": {
"1-3_years": "1-3 years",
"3-5_years": "3-5 years"
}
},
"conditional_question_3": {
"label": "Quantity you want to purchase",
"type": "CONDITIONAL",
"values": {
"1": "1",
"2": "2",
"3-5": "3-5",
"more_than_5": "more than 5"
}
},
"conditional_question_4": {
"label": "When do you plan to purchase",
"type": "CONDITIONAL",
"values": {
"in_upcoming_days_or_weeks": "In upcoming days or weeks",
"within_3_months": "Within 3 months",
"this_year": "This year",
"next_year": "Next year"
}
},
"what_type_of_business_do_you_operate?": {
"label": "What type of business do you operate?",
"values": {
"reseller": "Reseller",
"logistics_company": "Logistics company",
"other": "Other"
},
"type": "CUSTOM"
},
"company_name": {
"label": "Company name",
"type": "COMPANY_NAME",
"values": {}
},
"full_name": {
"label": "Full name",
"type": "FULL_NAME",
"values": {}
},
"phone_number": {
"label": "Phone number",
"type": "PHONE",
"values": {}
},
"email": {
"label": "Email",
"type": "EMAIL",
"values": {}
}
}
Ответы (1 шт):
Вариант с использованием стандартных библиотек. Глядя на json и на желаемый результат заметно, что словари собираются почти единообразно, поэтому:
- Напрашивается применение своей функции object_pairs_hook для рекурсивного декодирования json. Пример с OrderedDict:
with open("incoming.json", 'r') as file:
ordered_dict = json.load(file, object_pairs_hook=OrderedDict)
for d in ordered_dict['questions']:
print(d)
OrderedDict([('key', 'what_type_of_business_do_you_operate?'), ('label', 'What type of business do you operate?'), ('options', [OrderedDict([('key', 'reseller'), ('value', 'Reseller')]), OrderedDict([('key', 'logistics_company'), ('value', 'Logistics company')]), OrderedDict([('key', 'other'), ('value', 'Other')])]), ('type', 'CUSTOM'), ('id', '****************')])
OrderedDict([('key', 'conditional_question_1'), ('label', 'Please choose the truck brand your are interested in'), ('options', [OrderedDict([('key', 'volvo'), ('value', 'Volvo')]), OrderedDict([('key', 'mercedes-benz_actros'), ('value', 'Mercedes-Benz Actros')]), OrderedDict([('key', 'daf'), ('value', 'DAF')])]), ('type', 'CUSTOM'), ('dependent_conditional_questions', [OrderedDict([('name', 'Please choose age of the vehicle'), ('input_type', 'inline_select'), ('field_key', 'conditional_question_2')]), OrderedDict([('name', 'Quantity you want to purchase'), ('input_type', 'inline_select'), ('field_key', 'conditional_question_3')]), OrderedDict([('name', 'When do you plan to purchase'), ('input_type', 'inline_select'), ('field_key', 'conditional_question_4')])]), ('conditional_questions_choices', [OrderedDict([('value', 'Volvo'), ('customized_token', 'volvo'), ('next_question_choices', [OrderedDict([('value', '1-3 years'), ('customized_token', '1-3_years'), ('next_question_choices', [OrderedDict([('value', '1'), ('customized_token', '1'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '2'), ('customized_token', '2'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '3-5'), ('customized_token', '3-5'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', 'more than 5'), ('customized_token', 'more_than_5'), ('next_question_choices', [OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])])])]), OrderedDict([('value', '3-5 years'), ('customized_token', '3-5_years'), ('next_question_choices', [OrderedDict([('value', '1'), ('customized_token', '1'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '2'), ('customized_token', '2'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '3-5'), ('customized_token', '3-5'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', 'more than 5'), ('customized_token', 'more_than_5'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])])])])])]), OrderedDict([('value', 'Mercedes-Benz Actros'), ('customized_token', 'mercedes-benz_actros'), ('next_question_choices', [OrderedDict([('value', '1-3 years'), ('customized_token', '1-3_years'), ('next_question_choices', [OrderedDict([('value', '1'), ('customized_token', '1'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '2'), ('customized_token', '2'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '3-5'), ('customized_token', '3-5'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', 'more than 5'), ('customized_token', 'more_than_5'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])])])]), OrderedDict([('value', '3-5 years'), ('customized_token', '3-5_years'), ('next_question_choices', [OrderedDict([('value', '1'), ('customized_token', '1'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '2'), ('customized_token', '2'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '3-5'), ('customized_token', '3-5'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', 'more than 5'), ('customized_token', 'more_than_5'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])])])])])]), OrderedDict([('value', 'DAF'), ('customized_token', 'daf'), ('next_question_choices', [OrderedDict([('value', '3-5 years'), ('customized_token', '3-5_years'), ('next_question_choices', [OrderedDict([('value', '1'), ('customized_token', '1'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '2'), ('customized_token', '2'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', '3-5'), ('customized_token', '3-5'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])]), OrderedDict([('value', 'more than 5'), ('customized_token', 'more_than_5'), ('next_question_choices', [OrderedDict([('value', 'In upcoming days or weeks'), ('customized_token', 'in_upcoming_days_or_weeks')]), OrderedDict([('value', 'Within 3 months'), ('customized_token', 'within_3_months')]), OrderedDict([('value', 'This year'), ('customized_token', 'this_year')]), OrderedDict([('value', 'Next year'), ('customized_token', 'next_year')])])])])])])])]), ('conditional_questions_group_id', '1781381718904816'), ('id', '****************')])
OrderedDict([('key', 'company_name'), ('label', 'Company name'), ('type', 'COMPANY_NAME'), ('id', '****************')])
OrderedDict([('key', 'full_name'), ('label', 'Full name'), ('type', 'FULL_NAME'), ('id', '****************')])
OrderedDict([('key', 'phone_number'), ('label', 'Phone number'), ('type', 'PHONE'), ('id', '****************')])
OrderedDict([('key', 'email'), ('label', 'Email'), ('type', 'EMAIL'), ('id', '****************')])
На котором видно, что функция применилась ко всем, в том числе и вложенным словарям.
- Похоже, что OrderedDict пригодиться,так как нам нужно удалять ключи и собирать значения в новый словарь. Для сборки нового словаря из нужных элементов( пример без рекурсии):
new_dict = OrderedDict({d['key']: OrderedDict(islice(d.items(), 1, None)) for d in ordered_dict['questions']})
OrderedDict([('what_type_of_business_do_you_operate?', OrderedDict([('label', 'What type of business do you operate?'), ('options',....
видно, что нам нужно удалить из исходного словаря первый элемент и вставить его значение как ключ в новый словарь, а остаточный хвост (без первого элемента) - в значение. Может пойдет popitem(last = False), но в однострочнике у меня не получилось. Если запустить подобный код рекурсивно, выбирая только нужные элементы, то должен получиться нужный словарь. Может даже так:
new_dict = {d['key']: OrderedDict(islice(d.items(), 1, None)) for d in ordered_dict['questions']}
{'what_type_of_business_do_you_operate?': OrderedDict([('label', 'What type of business do you operate?'), ('options'...
сразу получается словарь.
Собрать полученные словари в один.
Преобразовать OrderedDict в простой словарь.
Конечно это только набросок, но буду рад если он поможет вам.