简体   繁体   English

如何合并2个JSON文件?

[英]How to merge 2 JSON files?

There are two different 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}
]

As you see, they have same information, but in different forms. 如您所见,它们具有相同的信息,但格式不同。 Some information is not in other file. 某些信息不在其他文件中。 I need to convert all of them into one postgreSQL table(or JSON file and parse it). 我需要将它们全部转换为一个postgreSQL表(或JSON文件并对其进行解析)。

So, what should I do to merge different formats JSON files into one file or one postgres table with Python? 那么,如何使用Python将不同格式的JSON文件合并为一个文件或一个postgres表呢?

You could just merge both into a single dictionary / json: 您可以将两者合并成一个字典/ json:

import json

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

print dic1

Output: 输出:

{
  "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
}

Note: the common keys will be overwritten by the values of the second file. 注意:公共密钥将被第二个文件的值覆盖。 So the order you read the files matters, based on what you need, change the order. 因此,根据需要,您读取文件的顺序很重要,请更改顺序。

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

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