-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathdict_to_url_params.py
82 lines (69 loc) · 2.44 KB
/
dict_to_url_params.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
def dict_to_url_params(json_data, root):
def deep(node, root, items):
if isinstance(node, list):
for i, value in enumerate(node):
node_root = root + f"[{i}]"
deep(value, node_root, items)
elif isinstance(node, dict):
for key, value in node.items():
node_root = root + f"[{key}]"
deep(value, node_root, items)
else:
root += "=" + node
items.append(root)
items = []
deep(json_data, root, items)
return items
if __name__ == "__main__":
JSON_FORM_DATA = {
"add": [
{
"source_name": "WEB сайт",
"source_uid": "a1fee7c0fc436088e64ba2e8822ba2b3",
"created_at": "1529007000",
"incoming_entities": {
"leads": [
{"name": "Покупка"},
],
"contacts": [
{
"name": "Федя",
"responsible_user_id": "1903006",
"custom_fields": [
{
"id": "382707",
"values": [
{"value": "+77777777777", "enum": "WORK"}
],
},
{
"id": "389993",
"values": [{"value": "sfgh3gh233h3h3h3"}],
},
{
"id": "389995",
"values": [{"value": "Обратный звонок"}],
},
],
}
],
},
"incoming_lead_info": {
"form_id": "329248",
"form_page": "vdtest.ru",
"ip": "127.0.0.1",
"service_code": "QkKwSam8",
},
}
]
}
items = dict_to_url_params(JSON_FORM_DATA["add"], root="add")
params = "&".join(items)
print("Params: " + params)
print()
print("Params:")
for x in items:
print(x)