I'm trying to print out a dataframe in json format. it's pretty cool so far because I can import a directory load of CSV files into a large dataframe, filter for the different criteria and create json outputs with select output from different filtered dataframes. Fantastic!
I'm really close BUT am seeing the forward slashes are escaped in the resulting output files.
I DO NOT want these escaped.
The command being 'j.to_json(path_to_dir + "filename.json",orient="records",force_ascii=False)'
I tried the 'force_ascii' True and False just to see what the difference would be. There is no difference. So basically, my output has a lot of units 'lb/hr' and similar. The application receiving and processing this json file has no provisions for this escaped forward slash.
Suggestions?
import pandas as pd
import json
path_to_dir = "C:\\"
suffix = "Example.csv"
dfi = pd.read_csv(path_to_dir + suffix)
dfo = pd.DataFrame()
dfo['tagName']=dfi['BNAME']
dfo['opcPath']= "{2*S*0:"+dfi['RNAME']+"}"
dfo['unit']=dfi['BUNITS']
dfo['isNumeric']='True'
dfo['name']=dfi['BNAME']
dfo['description']=dfi['BMESG']
dfo['type']='double-float'
dfo['title']=dfi['BMESG']
j = (dfo.groupby(['tagName','opcPath'],as_index=True)
.apply(lambda x: x[['unit','isNumeric','name','description','type','title']].to_dict('series'))
.reset_index())
j.rename(columns={0:'metadata'},inplace=True)
j.to_json("Example.json",orient="records",force_ascii=True)