How to merge several json files into one with Python

1 Answer

0 votes
import glob

with open("all_files.json", "w") as output:
    for f in glob.glob("*.json"):
        with open(f, "r") as input:
            line = None
            for line in input:
                output.write(line)



'''
data1.json
[
{"url": "https://www.collectivesolver.com/", "title": "Collective Solver"},
{"url": "https://www.collectivesolver.com/32116/how-to-split-a-string-in-c", "title": "How to split a string in C - Collective Solver"}
]
'''

'''
data2.json
[
{"url": "https://www.collectivesolver.com/32105/how-to-trim-a-string-in-c", "title": "How to trim a string in C - Collective Solver"},
{"url": "https://www.collectivesolver.com/32098/how-to-remove-all-spaces-from-a-string-in-c", "title": "How to remove all spaces from a string in C - Collective Solver"}
]
'''

# all_files.json

'''
run:

[
{"url": "https://www.collectivesolver.com/", "title": "Collective Solver"},
{"url": "https://www.collectivesolver.com/32116/how-to-split-a-string-in-c", "title": "How to split a string in C - Collective Solver"}
][
{"url": "https://www.collectivesolver.com/32105/how-to-trim-a-string-in-c", "title": "How to trim a string in C - Collective Solver"},
{"url": "https://www.collectivesolver.com/32098/how-to-remove-all-spaces-from-a-string-in-c", "title": "How to remove all spaces from a string in C - Collective Solver"}
]

'''

 



answered Jun 25, 2020 by avibootz

Related questions

1 answer 142 views
142 views asked Jul 8, 2020 by avibootz
2 answers 238 views
1 answer 135 views
1 answer 127 views
1 answer 146 views
1 answer 173 views
...