简体   繁体   中英

How to merge 2 JSON array files?

I have multiple files with JSON arrays from web scraping. They all look somewhat like this:

db_1.txt
[
{"title": "Title1", "price" : 21.37},
{"title": "Title2", "price" : 32.10},
{"title": "Title3", "price" : 221.67}
]

db_2.txt
[
{"title": "Title4", "price" : 121.37},
{"title": "Title5", "price" : 232.10}
]

How can I merge those files together while keeping same JSON array format? I obviously tried appending line by line with removing or adding ',' where needed but this is not probably as elegant and memory efficient way possible.

You could use the loads function of the json module that Python provides out of the box in order to read and process the json structures inside these .txt files and then append them to a final list in order to use it any way you want:

import json

result = []
textFiles = ['db_1.txt', 'db_2.txt']
for textFile in textFiles:
    with open(textFile, 'r') as file_1:
        data = json.loads(file_1.read())
        result.extend(data)

print(result)

This will print:

[{'title': 'Title1', 'price': 21.37}, {'title': 'Title2', 'price': 32.1}, {'title': 'Title3', 'price': 221.67}, {'title': 'Title4', 'price': 121.37}, {'title': 'Title5', 'price': 232.1}]

You could do it like this:

db_1.txt

{
    merged_files:[
          "tile1":"title2","price"
          "tilte3":"title4","price"
      ]
}


]

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