I’m encountering a problem while parsing a text file that contains data in JSON format.
Text file that we received from a customer
{“Item1″:”value1”, “Item2″:”value2”, “Item3″:”value3”, “Item4”: { “sub_Item1”: “sub_Value1”, “sub_Item2″:”sub_value2”, “sub_Item3″:”sub_value3”}}
2024-07-24T15:17:40.656-04:00
{
“Item1″:”value1”,
“Item2″:”value2”,
“Item3″:”value3”,
“Item4”:{
“sub_Item1”: “value1”,
“sub_Item2”: “value2”,
“sub_Item3”: “value3
}
}
{“Item1″:”value1”, “Item2″:”value2”, “Item3″:”value3”, “Item4”: { “sub_Item1”: “sub_Value1”, “sub_Item2″:”sub_value2”, “sub_Item3″:”sub_value3”}}
2024-07-24T15:17:40.656-04:00
{
“Item1″:”value1”,
“Item2″:”value2”,
“Item3″:”value3”,
“Item4”:{
“sub_Item1”: “value1”,
“sub_Item2”: “value2”,
“sub_Item3”: “value3
}
}
Continued to the rest of the file same as above
Desired Output:
[
{“Item1″:”value1”, “Item2″:”value2”, “Item3″:”value3”, “Item4”: { “sub_Item1”: “sub_Value1”, “sub_Item2″:”sub_value2”, “sub_Item3″:”sub_value3”}},
{
“Item1″:”value1”,
“Item2″:”value2”,
“Item3″:”value3”,
“Item4”:{
“sub_Item1”: “value1”,
“sub_Item2”: “value2”,
“sub_Item3”: “value3
}
},
{“Item1″:”value1”, “Item2″:”value2”, “Item3″:”value3”, “Item4”: { “sub_Item1”: “sub_Value1”, “sub_Item2″:”sub_value2”, “sub_Item3″:”sub_value3”}},
{
“Item1″:”value1”,
“Item2″:”value2”,
“Item3″:”value3”,
“Item4”:{
“sub_Item1”: “value1”,
“sub_Item2”: “value2”,
“sub_Item3”: “value3
}
},
Continued to transform same as above as JSON so that I can read JSON into Dataframe and load it to a table.
I tried json.load, json.loads, json.dump, json.dumps nothing is working out and ending up in extra double quotes around key: value paris and backlashes for each double quote in which not able to load it to DataFrame.
Sample code that I comeup with
i_file=’file.txt’
o_file=’file.json’
with open(i_file,’r’) as f:
for line in f:
data = [l.rstrip(‘n’) for l in line]
json.dump(data, o_file, ensure_ascii=False, indent=4)
result from the above code:
[“{“” “Item1″:”value1″,”,””Item2″:”value2″,” continued to the end of the file.
Your help in this matter would be sincerely appreciated.