I received a json from work and I need to put it on a pandas dataframe to analyze it. But Json came all weird missing quotes in some keys, well, I managed to solve this with but when I try to save the clean file (with the quotes) I have the error of in place of spaces I get "\"
The first json is like:
{"text":"CRD","qtip":"CRD","checked":false,"id":"5746p+5746!!DB::5746p","tsmax":1597788000000,"cost":{"202008":{"1":169.08978075830004,2:167.74736783330005,3:174.24878584530003,4:174.8258770385,5:173.49233388220006,6:174.67223572639998,7:175.13397411730006,8:170.16884950140002,9:170.46993024780002,10:177.9452736487,11:177.5172502943,12:176.8791710277,13:174.97972124289998,14:177.50405061630002,15:173.38104341860006,16:173.06225567280003,17:177.96418680850005,18:166.50419259929998,19:26.4,20:26.4,21:26.4,22:26.4,23:26.4,24:26.4,25:26.4,26:26.4,27:26.4,28:26.4,29:26.4,30:26.4,31:26.4,"avg":114.74620938931994,"estimate":5362.688623746617,"total":3468.7862816795982},"202006":{2:200.35115201620002,3:200.10138898519995,4:199.82156664749996,5:200.15923924589998,6:196.75398413409997,7:195.03808163369996,8:201.02971647659996,10:199.67497236949998,1:198.11742151040002,9:197.93282962139997,11:188.64197859260003,12:180.88797667890003,13:157.42718581050002,14:158.81001735070006,15:162.00267321069998,16:162.50923629150003,17:161.5225927354,18:160.28129821180008,19:159.21280188340003,20:159.87749734020005,21:161.2513404355,22:163.3872927245,23:162.49602354750004,24:161.9270906232,25:162.9849800909,26:165.5652523876,27:159.06988490620003,28:162.89904494350003,29:165.6445198728,30:165.63371783310004,"avg":175.70042534269976,"total":5271.012760280993},"202007"...
I solved the problem with this code below:
import re
regex = r"(\w+)\s?:\s?(\"?[^\",]+\"?,?)"
subst = "\"\\1\":\\2"
result = re.sub(regex, subst, test_str,0)
print (result)
[{"text":"CRD","qtip":"CRD","checked":false,"id":"5746p+5746!!"DB"::5746p","tsmax":1597788000000,"cost":{"202008":{"1":169.08978075830004,"2":167.74736783330005,"3":174.24878584530003,"4":174.8258770385,"5":173.49233388220006,"6":174.67223572639998,"7":175.13397411730006,"8":170.16884950140002,"9":170.46993024780002,"10":177.9452736487,"11":177.5172502943,"12":176.8791710277,"13":174.97972124289998,"14":177.50405061630002,"15":173.38104341860006,"16":173.06225567280003,"17":177.96418680850005,"18":166.50419259929998,"19":26.4,"20":26.4,"21":26.4,"22":26.4,"23":26.4,"24":26.4,"25":26.4,"26":26.4,"27":26.4,"28":26.4,"29":26.4,"30":26.4,"31":26.4,"avg":114.74620938931994,"estimate" ...
Now I want to save this 'str' as a json file, so i used this code:
with open('data.json', 'w', encoding='utf-8') as f:
test = json.dumps(result,separators=(',', ':'))
"\n[{\"text\":\"CRD\",\"qtip\":\"CRD\",\"checked\":false,\"id\":\"5746p+5746!!\"DB\"::5746p\",\"tsmax\":1597788000000,\"cost\":{\"202008\":{\"1\":169.08978075830004,\"2\":167.74736783330005,\"3\":174.24878584530003,\"4\":174.8258770385,\"5\":173.49233388220006,\"6\":174.67223572639998,\"7\":175.13397411730006,\"8\":170.16884950140002,\"9\":170.46993024780002,\"10\":177.9452736487,\"11\":177.5172502943,\"12\":176.8791710277,\"13\":174.97972124289998,\"14\":177.50405061630002,\"15\":173.38104341860006,\"16\":173.06225567280003,\"17\":177.96418680850005,\"18\":166.50419259929998,\"19\":26.4,\"20\":26.4,\"21\":26.4,\"22\":26.4,\"23\":26.4,\"24\":26.4,\"25\":26.4,\"26\":26.4,\"27\":26.4,\"28\":26.4,\"29\":26.4,\"30\":26.4,\"31\":26.4,\"avg\":114.74620938931994,\"estimate\":5362.688623746617,\"total\":3468.7862816795982},\"202006\":{\"2\":200.35115201620002,\"3\":200.10138898519995,\"4\":199...
How do I fix it? and how do I save the file correctly?