Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

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

2 answers 214 views
1 answer 135 views
135 views asked Jul 8, 2020 by avibootz
1 answer 122 views
1 answer 115 views
1 answer 126 views
1 answer 160 views
...