File size: 980 Bytes
3d4f9a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# convert json to single line string from all the folders where json is present

import os
import json
import glob
import argparse

def json2string(dir_path):
    for root, dirs, files in os.walk(dir_path):
        for file in files:
            if file.endswith('.json'):
                with open(os.path.join(root, file), 'r') as f:
                    # print the name of the file
                    print(file)
                    data = json.load(f)
                    data = json.dumps(data)
                    
                    # add double quotes to the string
                    print(data[:5])

                    # write to file
                    with open(os.path.join(root, file), 'w') as f:
                        f.write(data)

            
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--dir_path', type=str, default='.', help='Directory path')
    args = parser.parse_args()
    json2string(args.dir_path)