繁体   English   中英

如何合并2个JSON文件?

[英]How to merge 2 JSON files?

有两个不同的JSON文件。

[{"volume": "44", 
"affiliations": {}, 
"cite_count": 39, 
"issue": "12", 
"page_range": "1257-1271", 
"doi": "10.1016/0584-8547(89)80124-7", 
"title_en": "test"}
]

[{"sourceType": "Conference Proceeding", 
"page_range": "1257-1271",
"language": null, 
"volume": null, 
"titleEn": "test2", 
"spinCiteCount": null}
]

如您所见,它们具有相同的信息,但格式不同。 某些信息不在其他文件中。 我需要将它们全部转换为一个postgreSQL表(或JSON文件并对其进行解析)。

那么,如何使用Python将不同格式的JSON文件合并为一个文件或一个postgres表呢?

您可以将两者合并成一个字典/ json:

import json

dic1 = json.load('json_file_1')
dic2 = json.load('json_file_2')
dic1.update(dic2)

print dic1

输出:

{
  "volume": "44", 
  "affiliations": {}, 
  "cite_count": 39, 
  "issue": "12", 
  "page_range": "1257-1271", 
  "doi": "10.1016/0584-8547(89)80124-7", 
  "title_en": "test2"
  "sourceType": "Conference Proceeding", 
  "language": None, 
  "volume": None, 
  "spinCiteCount": None
}

注意:公共密钥将被第二个文件的值覆盖。 因此,根据需要,您读取文件的顺序很重要,请更改顺序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM