Search code examples
pythonjsonio

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.


Solution

  • 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}]