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

A R
  • 1,412
  • 2
  • 14
  • 29
xhaka
  • 11
  • 1
  • 1
  • 3
  • 1
    Start coding, see what happens. SO is about fixing _your_ Code - not implementing your ideas. Please go over [how to ask](https://stackoverflow.com/help/how-to-ask) and [on-topic](https://stackoverflow.com/help/on-topic) again and if you have questions provide your code as [mvce](https://stackoverflow.com/help/mcve). 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. – Patrick Artner Feb 09 '18 at 23:32
  • Dont forget research SO, f.e.: [how-to-merge-two-json-string-in-python](https://stackoverflow.com/questions/22698244/how-to-merge-two-json-string-in-python) and here:[reading-json-from-a-file 1](https://stackoverflow.com/questions/20199126/reading-json-from-a-file) and here: [parsing-values-from-a-json-file 2](https://stackoverflow.com/questions/2835559/parsing-values-from-a-json-file) and here: [python-read-json-file-and-modify](https://stackoverflow.com/questions/21035762/python-read-json-file-and-modify) or: https://stackoverflow.com/questions/12309269/how-do-i-write-json-data-to-a-file – Patrick Artner Feb 09 '18 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? – Robᵩ Feb 09 '18 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: https://www.digitalocean.com/community/tutorials/how-to-work-with-json-in-javascript. – jarmod Feb 09 '18 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 – xhaka Feb 09 '18 at 23:44

2 Answers2

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)
Matt. Stroh
  • 904
  • 7
  • 16
  • JSONDecodeError: Unterminated string starting at: line 1 column 143359 (char 143358) – xhaka Feb 09 '18 at 23:58
  • 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. – Matt. Stroh Feb 10 '18 at 00:02
  • I found the file corrupt, thanks to your code I solved everything, thanks again for your support! – xhaka Feb 10 '18 at 17:39
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)

  • 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](/help/how-to-answer). – Community Jun 10 '22 at 19:00