简体   繁体   中英

How to join (merge) several JSON files in Python

new here, starting learning programming on python and im currently working on a script to join several JSON files (generated via another API CALL script).

I have tried several solutions provided here (panda, merge, etc) and some of them work (they actually join the files) but the resulting file is either in incorrect format or the items are not in order.

The files im trying to join/merge are these: 2 JSON files

I really need to join/merge more than those 2 but i want to first make sure they are joined correctly and then i can write some code to join/merge the rest (they all have the same structure as these 2).

Thanks for your help.

edit: JSON files info (incomplete): json file 1:

{ "0": { "id_property": "0001" "address": "PH COLORES DE BELLA VISTA, BELLA VISTA", "area": "48", "availability_label": "Available", "bathrooms": "1", "bedrooms": "1",

"1": { "id_property": "0002" "address": "PH COLORES DE BELLA VISTA, BELLA VISTA", "area": "48", "availability_label": "Available", "bathrooms": "1", "bedrooms": "1",

"2": { "id_property": "0003" "address": "PH COLORES DE BELLA VISTA, BELLA VISTA", "area": "48", "availability_label": "Available", "bathrooms": "1", "bedrooms": "1", }, "status": "success", "total": 63 }

json file 2:

{ "0": { "id_property": "0004" "address": "PH COLORES DE BELLA VISTA, BELLA VISTA", "area": "48", "availability_label": "Available", "bathrooms": "1", "bedrooms": "1",

"1": { "id_property": "0005" "address": "PH COLORES DE BELLA VISTA, BELLA VISTA", "area": "48", "availability_label": "Available", "bathrooms": "1", "bedrooms": "1",

"2": { "id_property": "0006" "address": "PH COLORES DE BELLA VISTA, BELLA VISTA", "area": "48", "availability_label": "Available", "bathrooms": "1", "bedrooms": "1", }, "status": "success", "total": 66 }

I would like to join / merge them and end up with

merged json:

{ "0": { "id_property": "0001" "address": "PH COLORES DE BELLA VISTA, BELLA VISTA", "area": "48", "availability_label": "Available", "bathrooms": "1", "bedrooms": "1",

"1": { "id_property": "0002" "address": "PH COLORES DE BELLA VISTA, BELLA VISTA", "area": "48", "availability_label": "Available", "bathrooms": "1", "bedrooms": "1",

"2": { "id_property": "0003" "address": "PH COLORES DE BELLA VISTA, BELLA VISTA", "area": "48", "availability_label": "Available", "bathrooms": "1", "bedrooms": "1", "3": { "id_property": "0004" "address": "PH COLORES DE BELLA VISTA, BELLA VISTA", "area": "48", "availability_label": "Available", "bathrooms": "1", "bedrooms": "1",

"4": { "id_property": "0005" "address": "PH COLORES DE BELLA VISTA, BELLA VISTA", "area": "48", "availability_label": "Available", "bathrooms": "1", "bedrooms": "1",

"5": { "id_property": "0006" "address": "PH COLORES DE BELLA VISTA, BELLA VISTA", "area": "48", "availability_label": "Available", "bathrooms": "1", "bedrooms": "1", }, }

First load text files and convert to python dictionary:

import json

with open("file1.json"), "r") as f1:
    d1 = json.loads(f1.read())
with open("file2.json"), "r") as f2:
    d2 = json.loads(f2.read())

then join them:

d1.update(d2)

d1 is result, but d2 overwrite d1 if same keys exist in both dictionaries.

the generated json was a mess, and it was going to take a lot of work and time (for me) to achieve what i was trying to do. So i ended up taking a different approach, the system where i was pulling the json data from also offers a csv file via web, so i wrote a script with the use of selenium and chromedriver to log into my account and request the csv file. Then another script to log into my email and download the attached csv file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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