skip to Main Content

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
    ...

SCREENSHOT

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


  1. Chosen as BEST ANSWER

    In the end, I could fix my issue thanks to the contribution of @Zylesto:

    import json
    
    # Load the data from the JSON files
    with open('file1.json') as f1:
        data1 = json.load(f1)
    with open('file2.json') as f2:
        data2 = json.load(f2)
    
    # Concatenate the data
    result = data1
    index = len(data1)
    
    for k in range(len(data2)):
        result[str(index)] = data2[str(k)]
        index = index + 1
    
    # Save the combined data to a new JSON file
    with open('combinedFile.json', 'w') as outfile:
        json.dump(result, outfile)
    

  2. Try to not create a new DataFrame for each JSON file, but concatenate the data with a loop:

    import json
    
    # Load the data from the JSON files
    with open('file1.json') as f1:
        data1 = json.load(f1)
    with open('file2.json') as f2:
        data2 = json.load(f2)
    
    # Concatenate the data
    result = data1
    index = len(data1)
    
    for k in range(len(data2)):
        result[str(index)] = data2[str(k)]
        index = index + 1
    
    # Save the combined data to a new JSON file
    with open('combinedFile.json', 'w') as outfile:
        json.dump(result, outfile)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search