使用python合并两个json文件,每个文件具有多个json对象

h7appiyu  于 2021-09-08  发布在  Java
关注(0)|答案(2)|浏览(628)

我对python非常陌生,我需要基于“id”将两个json文件与多个json对象合并。
file1.json

{"id": 1, "name": "Ault", "class": 8, "email": "ault@pynative.com"}
{"id": 2, "name": "john", "class": 8, "email": "jhon@pynative.com"}
{"id": 3, "name": "josh", "class": 8, "email": "josh@pynative.com"}
{"id": 4, "name": "emma", "class": 8, "email": "emma@pynative.com"}

file2.json

{"id": 4, "math": "A", "class": 8, "physics": "D"}
{"id": 2, "math": "B", "class": 8, "physics": "C"}
{"id": 3, "math": "A", "class": 8, "physics": "A"}
{"id": 1, "math": "C", "class": 8, "physics": "B"}

我尝试了json.load(jsonobj)和json.load(path)。两者都会抛出错误。我知道这两个文件都不是一个有效的json整体(组合),但文件中的每一行都是一个有效的json。我想逐行阅读,并将两者合并。

ufj5ltwl

ufj5ltwl1#

您可以逐行读取,然后解析


# asuming

# File1.json

# {"id": 1, "name": "Ault", "class": 8, "email": "ault@pynative.com"}

# {"id": 2, "name": "john", "class": 8, "email": "jhon@pynative.com"}

# {"id": 3, "name": "josh", "class": 8, "email": "josh@pynative.com"}

# {"id": 4, "name": "emma", "class": 8, "email": "emma@pynative.com"}

# File2.json

# {"id": 4, "math": "A", "class": 8, "physics": "D"}

# {"id": 2, "math": "B", "class": 8, "physics": "C"}

# {"id": 3, "math": "A", "class": 8, "physics": "A"}

# {"id": 1, "math": "C", "class": 8, "physics": "B"}

import json
merged = {}

with open('File1.json') as f:
    for line in f:
        jsonified = json.loads(line)
        merged[jsonified['id']] = jsonified

with open('File2.json') as f:
    for line in f:
        jsonified = json.loads(line)
        merged[jsonified['id']].update(jsonified) # asuming both file has same ids otherwise use try catch

merged = list(merged.values())
print(merged)
[{'id': 1,
  'name': 'Ault',
  'class': 8,
  'email': 'ault@pynative.com',
  'math': 'C',
  'physics': 'B'},
 {'id': 2,
  'name': 'john',
  'class': 8,
  'email': 'jhon@pynative.com',
  'math': 'B',
  'physics': 'C'},
 {'id': 3,
  'name': 'josh',
  'class': 8,
  'email': 'josh@pynative.com',
  'math': 'A',
  'physics': 'A'},
 {'id': 4,
  'name': 'emma',
  'class': 8,
  'email': 'emma@pynative.com',
  'math': 'A',
  'physics': 'D'}]
rqenqsqc

rqenqsqc2#

这是我对Pandas的看法。

import pandas as pd
import os

os.chdir(os.getcwd())
file_path_1 = 'file1.json'
file_path_2 = 'file2.json'

df1 = pd.read_json(file_path_1, lines=True)
df2 = pd.read_json(file_path_2, lines=True)

df = df1.merge(df2, on='id')

print(df)

输出:

id  name  class_x              email math  class_y physics
0   1  Ault        8  ault@pynative.com    C        8       B
1   2  john        8  jhon@pynative.com    B        8       C
2   3  josh        8  josh@pynative.com    A        8       A
3   4  emma        8  emma@pynative.com    A        8       D

相关问题