Consider this code, which is understandably failing:
def testDataTransform():
source = 'data/threads/testFile2.json'
newFile = 'data/threads/testFile1.json'
jX = returnJson(source)
jY = returnJson(newFile)
for dataL1 in jX:
#print dataL1['city']
for dataL2 in jY:
if dataL1['city'] == dataL2['city']:
dataL2.append(dataL1['population'])
print dataL2
# end testDataTransform
def returnJson(source):
#Grab destination file json
try:
with open(source) as jsonFile: # Open and verify file is there
# load JSON object into memory
j = json.load(jsonFile)
return j
except Exception, e:
print e
raise
# end returnJson
testDataTransform()
The error generated is: AttributeError: 'dict' object has no attribute 'append',
which I now understand is due to trying to use append on a file steam and thus appending my JSON incorrectly. The question is how would I do this correctly? I want to add population data, which is in the source file.
JSON structure in testFiles look like this:
[
{
"city": "New York",
"lat": 20.1234,
"long": 32.09876
},
{
"city": "London",
"lat": 21.1234,
"long": 37.09876
},
{
"city": "New Jersey",
"lat": 10.1234,
"long": 30.09876
}
]
One has a "population" node and other does not. I want to copy that population data from one file to another. How is that done? I wonder if converting the file stream into an array would help, but how do I even do that?
Thanks.