下面的代码功能齐全并执行其任务。
对在不使用第三方库的情况下对其进行优化或实施的可能性感兴趣。
提前感谢您的建设性建议。
编码:
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": {}
}
}