Home > other >  Combine Json Files with a Continuing Index
Combine Json Files with a Continuing Index

Time:01-03

Json file sample: 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!

CodePudding user response:

Try to not create a new DataFrame for each JSON file, but concatenate the data directly:


# Load the data from the JSON files
with open('Belem_data_p1.json') as f1:
    data1 = json.load(f1)
with open('Belem_data_p2.json') as f2:
    data2 = json.load(f2)

# Concatenate the data
data = data1   data2

# Save the combined data to a new JSON file
with open('NewFileName.json', 'w') as outfile:
    json.dump(data, outfile)

CodePudding user response:

Try this:

import json
import pandas as pd


#Call your first file instead of string
JSON1=json.loads('''[
    {"Title":"A","Location":"B","Price":"C","Net area":"D","Gross area":"E"},
    {"Title":"A","Location":"B","Price":"C","Net area":"D","Gross area":"E"},
    {"Title":"A","Location":"B","Price":"C","Net area":"D","Gross area":"E"},
    {"Title":"A","Location":"B","Price":"C","Net area":"D","Gross area":"E"},
    {"Title":"A","Location":"B","Price":"C","Net area":"D","Gross area":"E"},
    {"Title":"A","Location":"B","Price":"C","Net area":"D","Gross area":"E"}
]''')


#Call your second file instead of string
JSON2=json.loads('''[
    {"Title":"Z","Location":"Y","Price":"X","Net area":"W","Gross area":"V"},
    {"Title":"Z","Location":"Y","Price":"X","Net area":"W","Gross area":"V"},
    {"Title":"Z","Location":"Y","Price":"X","Net area":"W","Gross area":"V"},
    {"Title":"Z","Location":"Y","Price":"X","Net area":"W","Gross area":"V"}
]''')


df=pd.DataFrame(data=(JSON1 JSON2))

print((df.index))
  • Related