0

How to get rid of the string format for the data from the JSON format. for example :

json = [{"data" :"abc", "data1" :{"a" : 1,"b":2}, "data2" :{"c" : 3, "d":4}}]

what I am getting is

{"data" :"abc", "data1" :"{"a" : 1,"b":2}", "data2":"{"c" : 3, "d":4}"}

I dont want this " " in the dictionary for values ("{"a" : 1,"b":2}")

the code I'm using to get the data from the CSV is shown below

df = pd.read_csv('gen.csv')  
data = df.to_dict('records')
print(data)

enter image description here

sreevalsan
  • 33
  • 5
  • 2
    How your csv looks like? – lojza Apr 14 '22 at 07:40
  • 3
    The string that you posted after "What I am getting is" is not a valid python statement. – SuperNoob Apr 14 '22 at 07:48
  • @SuperNoob Sorry that was a mistake. Now I changed... please look into it. – sreevalsan Apr 15 '22 at 04:20
  • @lojza CSV file will be looks like **data** as header and "abc" as cell below the data header. data1 is another header, "{a:1,b:2}" is the value for that header.... data2 is another header, and "{"c":3,"d":4}" is value for that header. – sreevalsan Apr 15 '22 at 04:24
  • @Sreevalsan You are still not being clear. Post your gen.csv file. And how are you getting this: ```{"data" :"abc", "data1" :"{"a" : 1,"b":2}", "data2":"{"c" : 3, "d":4}"}``` line? And what do you expect this line to be? – SuperNoob Apr 15 '22 at 07:00
  • Check https://stackoverflow.com/questions/20680272/parsing-a-json-string-which-was-loaded-from-a-csv-using-pandas – lojza Apr 15 '22 at 17:16
  • @lojza I don't have a problem as the CSV is loaded with the data like that. But while reading it back, I'm getting it all in a string format. I want the data, how I appended it. Here dictionary is also getting in a string format. I don't want that. Is there any way that I can read or format is back to the original ? – sreevalsan Apr 18 '22 at 04:57

0 Answers0