UPDATE: Json Files:
LINK
I have a multitude of Json files that have the same individual structure:
0:
Title
Location
Price
Net area
Gross area
...
1:
Title
Location
...
2:
...
One Json file may run from index 0: to 56:, while another may run from 0: to 60:. I have endlessly sought to combine them with .concat, .join, and .merge but the final Json file never is what I expected. Combining 0: to 56: with 0:to 60 should give a new Json file running from 0: to 118 (index). But at best I get the following structure:
0:
0:
Title
...
1:
Title
...
1:
0:
Title
...
1:
Title
...
2:
0:
Title
...
1:
Title
...
Here is the code I used:
import json
import pandas as pd
with open('Belem_data_p1.json') as f1:
data1=json.load(f1)
with open('Belem_data_p2.json') as f2:
data2=json.load(f2)
df1=pd.DataFrame([data1])
df2=pd.DataFrame([data2])
MergeJson=pd.concat([df1,df2]).reset_index()
MergeJson.to_json('NewFileName.json')
print (MergeJson)
I need the combination of DataFrames to update the index so that my data appears as such:
0:
Title
Location
...
[...]
118:
Title
Location
...
SCREENSHOT
I’d be so thankful for a fix, because I’ve been searching endlessly and couldn’t find the solution!
EDITS:
NIYAZ
Link to the newly produced Json file: LINK
Code ran:
import json
import pandas as pd
with open('Belem_data_p1.json') as f1:
data1=json.load(f1)
with open('Belem_data_p2.json') as f2:
data2=json.load(f2)
df=pd.DataFrame(data=(list(data1.values())+list(data2.values())))
print((len(df)))
df.tail(10)
df.to_json('ImYourJson44.json')
ZYLESTO
New file produced: LINK
Code ran:
import json
with open('Belem_data_p1.json') as f1:
data1=json.load(f1)
with open('Belem_data_p2.json') as f2:
data2=json.load(f2)
data=dict(data1)
data.update(data2)
with open('Belem_FINAL.json','w') as outfile:
json.dump(data,outfile)
2
Answers
In the end, I could fix my issue thanks to the contribution of @Zylesto:
Try to not create a new DataFrame for each JSON file, but concatenate the data with a loop: