1

The problem :

I have a series of files in a folder json_data = open("C:/Users/Desktop/soccer_data2/*.json")

like that:

a-01.json
a-02.json
a-03.json

a-01.json :

{'blabla': '127',
 'blabla': 'Sun,,26,Oct,2014',
 'events': [{'outcome': 'save',
             'playerId': 124,
             'position': ['0,50'],
             'teamId': 16,
             'timestamp': 294,
             'type': 'goal_keeping'},
            {'outcome': 'save',
             'playerId': 434,
             'position': ['0,50'],
             'teamId': 19,
             'timestamp': 744,
             'type': 'goal_keeping'},


a-02.json :
{'away_team': '112',
 'date': 'Sun,,27,Oct,2014',
 'events': [{'outcome': 'save',
             .

And i want to merge all files in one json. It is possible? thanks to all

5
  • 1
    Start coding, see what happens. SO is about fixing your Code - not implementing your ideas. Please go over how to ask and on-topic again and if you have questions provide your code as mvce. If you encounter errors, copy and paste the error message verbatim ( word for word) into your question. Avoid using screenshots unless you need to convey layout errors. We can NOT copy and paste your image into our IDEs to fix your code. Commented Feb 9, 2018 at 23:32
  • Dont forget research SO, f.e.: how-to-merge-two-json-string-in-python and here:reading-json-from-a-file 1 and here: parsing-values-from-a-json-file 2 and here: python-read-json-file-and-modify or: stackoverflow.com/questions/12309269/… Commented Feb 9, 2018 at 23:34
  • Also, your problem statement is ambiguous. What, precisely, do you mean by "merge"? Do you want a list where each object in the list represents one of the original files? Do you want a dictionary mapping the filenames into their contents? Do you want a single dictionary which is the result of calling dict.update() for each file? Commented Feb 9, 2018 at 23:36
  • Find out how to use the fs module (to read/write files) and the json module (to convert objects to/from JSON). Also read this for background: digitalocean.com/community/tutorials/…. Commented Feb 9, 2018 at 23:38
  • My problem would be to load the files one after the other in sequence (with a for loop, if possible), and maybe after concatenating them Commented Feb 9, 2018 at 23:44

2 Answers 2

4

This is just a template that I wrote here without testing it, so it might have some bugs or typo, if anyone have comments I will appreciate it.

import os # for manipulates files and subdirectories
import json # handle json files

json_folder_path = os.path.join("C","Users","Desktop","soccer_data2")
# In order to get the list of all files that ends with ".json"
# we will get list of all files, and take only the ones that ends with "json"
json_files = [ x for x in os.listdir(json_folder_path) if x.endswith("json") ]
json_data = list()
for json_file in json_files:
    json_file_path = os.path.join(json_folder_path, json_file)
    with open (json_file_path, "r") as f:
        json_data.append(json.load(f))

# now after iterate all the files, we have the data in the array, so we can just write it to file
output_path = os.path.join(json_folder_path,"output.json")
with open (output_path, "w") as f:
    json.dump(json_data, f)
Sign up to request clarification or add additional context in comments.

3 Comments

JSONDecodeError: Unterminated string starting at: line 1 column 143359 (char 143358)
I believe that reason you are failing now is because your json files have issues. but copy&paste for the errors you have won't help you, search in Goggle before.
I found the file corrupt, thanks to your code I solved everything, thanks again for your support!
0

I have tested the below and it worked.

import os,json

path_to_json = 'C:/PR/1/'

for file_name in [file for file in os.listdir(path_to_json) if file.endswith('.json')]: with open(path_to_json + file_name) as json_file: data = json.load(json_file) print(data)

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.