This is the function
def update_content_if_reusable(content_data):
""" Add reusable counter """
content = json.loads(content_data)
if 'layouts' in content:
for layout in content['layouts'].values():
if 'isReusable' in layout:
content['reusableBlocks'] += 1
return json.dumps(content, indent=4)
The error in test
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
content_data = '"{\\n \\"page\\": {\\n \\"layouts\\": []\\n },\\n \\"reusableBlocks\\": 0,\\n \\"layouts\\": {},\\n \\"columns\\": {},\\n \\"components\\": {}\\n}"'
def update_content_if_reusable(content_data):
""" Add reusable counter """
content = json.loads(content_data)
if 'layouts' in content:
> for layout in content['layouts'].values():
E TypeError: string indices must be integers
models.py:82: TypeError
The python version is 3.6
https://www.online-python.com/5VH28jsWYQ
>Solution :
The issue is that you are passing in a JSON string that represents an actual string that contains the data you want. You would need to call json.loads twice to get the dictionary out of that.
update_content_if_reusable(json.loads(content_data))