I want to generate json file.
My steps are:
- setup configuration using lists
- loop and build dictionary
- append dictionary to list
- save list as json
I can replicate issue using snippet below:
import json
myList = ['a', 'b', 'c']
number = 1
jsonList = list()
jsonDict = dict()
for name in myList:
jsonDict["key"] = "Value:" + str(number)
jsonDict["Letter"] = "Letter:" + str(name)
print(jsonDict)
jsonList.append(jsonDict)
number = number + 1
print(json.dumps(jsonList))
Issue: jsonDict has correct values such as I planned:
{'key': 'Value:1', 'Letter': 'Letter:a'}
{'key': 'Value:2', 'Letter': 'Letter:b'}
{'key': 'Value:3', 'Letter': 'Letter:c'}
However after converting list to json string it becomes:
[{"key": "Value:3", "Letter": "Letter:c"},
{"key": "Value:3", "Letter": "Letter:c"},
{"key": "Value:3", "Letter": "Letter:c"}]