Converting Single string quote into JSON and running on bunch of files
- bdata3
- Oct 21, 2020
- 1 min read
Updated: Nov 15, 2020
So you have a file like json but with single quote and you want to load it to mongodb or just to have a valid Json ....
import ast,json
new_json = json.dumps(ast.literal_eval("{'name':'geva','lang':'python'}"))
and lets assume you want to convert a bunch of files in the format [{'name':'geva', 'lang':'python'},{'name':'sela','lang':'The language of the hebrew man'}] into files of json in a line you can use :
import json,ast,glob,os
path='/home/ec2-user/code/data/*.txt'
list_files=glob.glob(path)
for i in list_files:
with open (i) as in_f, open(os.path.splitext(i)[0]+'.json','w') as o_f:
[o_f.write(json.dumps(x)+'\n') for x in ast.literal_eval(in_f.read())]
and your done

Comments