You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

合并JSON文件并实现索引连续递增的技术求助

问题:合并同结构JSON文件并生成连续索引的新文件

更新: 提供了相关JSON文件

我有多个结构相同的JSON文件,每个文件的结构如下:

0:
 Title
 Location
 Price
 Net area
 Gross area
 ...

1: 
 Title
 Location
 ...

2: 
 ...

其中一个JSON文件的索引范围是0到56,另一个是0到60。我尝试用.concat、.join和.merge方法合并它们,但最终的JSON文件始终不符合预期。将0到56与0到60的文件合并后,应该得到索引从0到118的新JSON文件,但最多只能得到如下嵌套结构:

0: 
  0:
    Title
    ...
  1:
    Title
    ...
1:
  0: 
    Title
    ...
  1: 
    Title
    ...
2:
  0:
    Title
    ...
  1:
    Title
    ...

我使用的代码如下:

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)

我需要合并后更新索引,使数据呈现如下结构:

0: 
  Title
  Location
  ...

[...]

118:
 Title
 Location
 ...

我已经搜索了很久都没找到办法,非常感谢能得到解决方案!


补充内容

NIYAZ提供的解决方案

运行代码如下:

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提供的解决方案

运行代码如下:

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)

内容的提问来源于stack exchange,提问作者Dan_San

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 12:05:42