Hi all,
-- From below json data to parse this data to fetch categoryId and categoryName in four levels of data I used for loop for fetching data and inserting into database table. Is there any better way to do this in python and Django?
Please Help me to achieve this. Let me know if you have any suggestions.
```
response = requests.request("GET", url, headers=headers_name, data=payload)
response.json()
json_encoded_e = json.dumps(response.json(), indent=4)
json_decode_e = json.loads(json_encoded_e)
categoryTreeId = json_decode_e["categoryTreeId"]
categoryTreeVersion = json_decode_e["categoryTreeVersion"]
rootCategoryNode = json_decode_e["rootCategoryNode"]
json_encoded_e1 = json.dumps(rootCategoryNode, indent=4)
json_decode_e1 = json.loads(json_encoded_e1)
category_1 = json_decode_e1["childCategoryTreeNodes"]
for i in category_1:
cat_id = i['category']['categoryId']
cat_name = i['category']['categoryName']
category_child = i['childCategoryTreeNodes']
saveForm = Some_data_table(categoryTreeId=categoryTreeId,
categoryTreeVersion=categoryTreeVersion,
categoryId=cat_id, categoryName=cat_name,
parent_id=cat_id, parent_name=cat_name,
)
saveForm.save()
for child in category_child:
cat_ch_id = child['category']['categoryId']
cat_ch_name = child['category']['categoryName']
saveForm = Some_data_table(categoryTreeId=categoryTreeId,
categoryTreeVersion=categoryTreeVersion,
categoryId=cat_ch_id, categoryName=cat_ch_name,
parent_id=cat_id, parent_name=cat_name,
)
saveForm.save()
if 'childCategoryTreeNodes' in child:
cat_subch= child['childCategoryTreeNodes']
for subch in cat_subch:
cat_subch_id = subch['category']['categoryId']
cat_subch_name = subch['category']['categoryName']
saveForm = Some_data_table(categoryTreeId=categoryTreeId,
categoryTreeVersion=categoryTreeVersion,
categoryId=cat_subch_id, categoryName=cat_subch_name,
parent_id=cat_ch_id, parent_name=cat_ch_name,
)
saveForm.save()
if 'childCategoryTreeNodes' in subch:
cat_subch_subch = subch['childCategoryTreeNodes']
for subch_subch in cat_subch_subch:
cat_subch_subch_id = subch_subch['category']['categoryId']
cat_subch_subch_name = subch_subch['category']['categoryName']
saveForm = Some_data_table(categoryTreeId=categoryTreeId,
categoryTreeVersion=categoryTreeVersion,
categoryId=cat_subch_subch_id,
categoryName=cat_subch_subch_name,
parent_id=cat_subch_id, parent_name=cat_subch_name,
)
saveForm.save()
else:
continue
else:
continue
response.json()
json_encoded_e = json.dumps(response.json(), indent=4)
json_decode_e = json.loads(json_encoded_e)
categoryTreeId = json_decode_e["categoryTreeId"]
categoryTreeVersion = json_decode_e["categoryTreeVersion"]
rootCategoryNode = json_decode_e["rootCategoryNode"]
json_encoded_e1 = json.dumps(rootCategoryNode, indent=4)
json_decode_e1 = json.loads(json_encoded_e1)
category_1 = json_decode_e1["childCategoryTreeNodes"]
for i in category_1:
cat_id = i['category']['categoryId']
cat_name = i['category']['categoryName']
category_child = i['childCategoryTreeNodes']
saveForm = Some_data_table(categoryTreeId=categoryTreeId,
categoryTreeVersion=categoryTreeVersion,
categoryId=cat_id, categoryName=cat_name,
parent_id=cat_id, parent_name=cat_name,
)
saveForm.save()
for child in category_child:
cat_ch_id = child['category']['categoryId']
cat_ch_name = child['category']['categoryName']
saveForm = Some_data_table(categoryTreeId=categoryTreeId,
categoryTreeVersion=categoryTreeVersion,
categoryId=cat_ch_id, categoryName=cat_ch_name,
parent_id=cat_id, parent_name=cat_name,
)
saveForm.save()
if 'childCategoryTreeNodes' in child:
cat_subch= child['childCategoryTreeNodes']
for subch in cat_subch:
cat_subch_id = subch['category']['categoryId']
cat_subch_name = subch['category']['categoryName']
saveForm = Some_data_table(categoryTreeId=categoryTreeId,
categoryTreeVersion=categoryTreeVersion,
categoryId=cat_subch_id, categoryName=cat_subch_name,
parent_id=cat_ch_id, parent_name=cat_ch_name,
)
saveForm.save()
if 'childCategoryTreeNodes' in subch:
cat_subch_subch = subch['childCategoryTreeNodes']
for subch_subch in cat_subch_subch:
cat_subch_subch_id = subch_subch['category']['categoryId']
cat_subch_subch_name = subch_subch['category']['categoryName']
saveForm = Some_data_table(categoryTreeId=categoryTreeId,
categoryTreeVersion=categoryTreeVersion,
categoryId=cat_subch_subch_id,
categoryName=cat_subch_subch_name,
parent_id=cat_subch_id, parent_name=cat_subch_name,
)
saveForm.save()
else:
continue
else:
continue
```
```
{'categoryTreeId': '0',
'categoryTreeVersion': 'some',
'rootCategoryNode': {'category': {'categoryId': '0', 'categoryName': 'Root'},
'childCategoryTreeNodes':
[{'category': {'categoryId': '1', 'categoryName': 'Collectibles'},
'parentCategoryTreeNodeHref': 'https://api.example.com/',
'childCategoryTreeNodes':
[{'category': {'categoryId': '34', 'categoryName': 'Advertising'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=1',
'childCategoryTreeNodes':
[{'category': {'categoryId': '36', 'categoryName': 'Soda'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=34',
'childCategoryTreeNodes':
[{'category': {'categoryId': '852', 'categoryName': 'Fountain'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=36',
'categoryTreeNodeLevel': 4, 'leafCategoryTreeNode': True},
{'category': {'categoryId': '853', 'categoryName': 'Canada Dry'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=36',
'categoryTreeNodeLevel': 4, 'leafCategoryTreeNode': True},
{'category': {'categoryId': '855', 'categoryName': '7 UP'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=36',
'categoryTreeNodeLevel': 4, 'leafCategoryTreeNode': True},
{'category': {'categoryId': '856', 'categoryName': 'Squirt'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=36',
'categoryTreeNodeLevel': 4, 'leafCategoryTreeNode': True},
{'category': {'categoryId': '857', 'categoryName': 'Hires Root Beer'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=36',
'categoryTreeNodeLevel': 4, 'leafCategoryTreeNode': True},
{'category': {'categoryId': '877', 'categoryName': 'Dr Pepper'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=36',
'categoryTreeNodeLevel': 4, 'leafCategoryTreeNode': True},
{'category': {'categoryId': '1334', 'categoryName': 'Moxie'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=36',
'categoryTreeNodeLevel': 4, 'leafCategoryTreeNode': True},
{'category': {'categoryId': '3897', 'categoryName': 'Schweppes'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=36',
'categoryTreeNodeLevel': 4,
'leafCategoryTreeNode': True},
-----------------------------------------------------------------
-------------------------------------------------------------------
------------------------------------------------------------------
```
'categoryTreeVersion': 'some',
'rootCategoryNode': {'category': {'categoryId': '0', 'categoryName': 'Root'},
'childCategoryTreeNodes':
[{'category': {'categoryId': '1', 'categoryName': 'Collectibles'},
'parentCategoryTreeNodeHref': 'https://api.example.com/',
'childCategoryTreeNodes':
[{'category': {'categoryId': '34', 'categoryName': 'Advertising'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=1',
'childCategoryTreeNodes':
[{'category': {'categoryId': '36', 'categoryName': 'Soda'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=34',
'childCategoryTreeNodes':
[{'category': {'categoryId': '852', 'categoryName': 'Fountain'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=36',
'categoryTreeNodeLevel': 4, 'leafCategoryTreeNode': True},
{'category': {'categoryId': '853', 'categoryName': 'Canada Dry'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=36',
'categoryTreeNodeLevel': 4, 'leafCategoryTreeNode': True},
{'category': {'categoryId': '855', 'categoryName': '7 UP'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=36',
'categoryTreeNodeLevel': 4, 'leafCategoryTreeNode': True},
{'category': {'categoryId': '856', 'categoryName': 'Squirt'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=36',
'categoryTreeNodeLevel': 4, 'leafCategoryTreeNode': True},
{'category': {'categoryId': '857', 'categoryName': 'Hires Root Beer'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=36',
'categoryTreeNodeLevel': 4, 'leafCategoryTreeNode': True},
{'category': {'categoryId': '877', 'categoryName': 'Dr Pepper'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=36',
'categoryTreeNodeLevel': 4, 'leafCategoryTreeNode': True},
{'category': {'categoryId': '1334', 'categoryName': 'Moxie'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=36',
'categoryTreeNodeLevel': 4, 'leafCategoryTreeNode': True},
{'category': {'categoryId': '3897', 'categoryName': 'Schweppes'},
'parentCategoryTreeNodeHref': 'https://api.example.com/category_id=36',
'categoryTreeNodeLevel': 4,
'leafCategoryTreeNode': True},
-----------------------------------------------------------------
-------------------------------------------------------------------
------------------------------------------------------------------
```
Thanks
~Salima
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMSz6b%3DycVNap0Xm7MWhL6iaB5d%2B0UgNUA9rRwh%3DhLpG3XVi7g%40mail.gmail.com.
No comments:
Post a Comment