Replace data in json file with a dataframe
I have a json file in the following format
"rawDataBody":
{
"data": [
{
"name": "Test",
"unit": "",
"format": "integer",
"key": "Test"
}
],
"dataBlock": [
[7, 1730569828, 3490, 1608636960, 30.62, 1003.82, 44.14, 683806.38, 2, 1, 0, 0],
[0, 1730563432, 3545, 1608636960, 29.89, 1003.52, 39.25, 557582.38, 2, 1, 0, 0],
[1, 1730579048, 3571, 1608636960, 29.79, 1003.45, 41.07, 494566.53, 2, 1, 0, 0],
[2, 1730568292, 3595, 1608636960, 29.62, 1003.40, 42.72, 546424.75, 2, 1, 0, 0],
I want to replace the data in the dataBlock with my dataframe and extract the whole in json file
5.0,1730566755.0,22.11,1608636969.0,33.42,1003.5,39.78,60591.71,9.0,1.0,0.0,0.0
6.0,1730551139.0,22.14,1608636969.0,33.27,1003.64,38.77,77906.27,9.0,1.0,0.0,0.0
6.0,1730551139.0,22.14,1608636969.0,33.27,1003.64,38.77,77906.27,9.0,1.0,0.0,0.0
5.0,1730566755.0,42.11,1608636969.0,33.42,1003.5,39.78,60591.71,9.0,1.0,0.0,0.0
The code i have tried is. I dont know why this is not working.can someone help me
with open('data.txt', 'r') as file:
jsonData = json.load(file)
dfFinalResult = dfFinalResult.values.tolist()
for item in jsonData['rawDataBody']['dataBlock']:
item = dfFinalResult
with open('newjsonfile.txt', 'w') as file:
json.dump(jsonData, file)
1 answer
-
answered 2021-01-19 12:18
quamrana
You seem to be saying that my suggestion worked.
Here is what I think you should have:
with open('data.txt', 'r') as file: jsonData = json.load(file) dfFinalResult = dfFinalResult.values.tolist() jsonData['rawDataBody']['dataBlock'] = dfFinalResult with open('newjsonfile.txt', 'w') as file: json.dump(jsonData, file)