looked at previous answers but cant solve it, the json.loads wont work.
the code:
import json
import operator
Data_to_python ={}
Bank_Data_note= open('Data_for_python.txt','r') # open file
Bank_Data_str = Bank_Data_note.read()
print(Bank_Data_str)
Data_to_python =json.loads(Bank_Data_str) # dictinary
print(Data_to_python)
the json format from text file:
{{"Transaction_1":{"Name":"Magnolia","Location":"Ayilon male","Amount":289,"Date":"5/5/18"},
{"Transaction_2":{"Name":"Landver,"Location":"Cinima-city Ramat-hashron","Amount":15,"Date":"15/5/18"},
{"Transaction_3":{"Name":"Superfarm","Location":"Shivat-hacochvim male","Amount":199,"Date":"7/5/18"},
{"Transaction_4":{"Name":"Printing solutions","Location":"Afeka tel-aviv","Amount":16,"Date":"25/5/18"}}
I got this:
obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
You had too many braces in your JSON data! And it was missing a double-quote. Here's a repaired version, along with some json
loading and dumping code to test it.
import json
data = '''
{
"Transaction_1": {"Name":"Magnolia","Location":"Ayilon male","Amount":289,"Date":"5/5/18"},
"Transaction_2": {"Name":"Landver","Location":"Cinima-city Ramat-hashron","Amount":15,"Date":"15/5/18"},
"Transaction_3": {"Name":"Superfarm","Location":"Shivat-hacochvim male","Amount":199,"Date":"7/5/18"},
"Transaction_4": {"Name":"Printing solutions","Location":"Afeka tel-aviv","Amount":16,"Date":"25/5/18"}
}'''
obj = json.loads(data)
print(obj)
print('- ' * 20)
# Convert back to JSON for nicer printing
print(json.dumps(obj, indent=4))
output
{'Transaction_1': {'Name': 'Magnolia', 'Location': 'Ayilon male', 'Amount': 289, 'Date': '5/5/18'}, 'Transaction_2': {'Name': 'Landver', 'Location': 'Cinima-city Ramat-hashron', 'Amount': 15, 'Date': '15/5/18'}, 'Transaction_3': {'Name': 'Superfarm', 'Location': 'Shivat-hacochvim male', 'Amount': 199, 'Date': '7/5/18'}, 'Transaction_4': {'Name': 'Printing solutions', 'Location': 'Afeka tel-aviv', 'Amount': 16, 'Date': '25/5/18'}}
- - - - - - - - - - - - - - - - - - - -
{
"Transaction_1": {
"Name": "Magnolia",
"Location": "Ayilon male",
"Amount": 289,
"Date": "5/5/18"
},
"Transaction_2": {
"Name": "Landver",
"Location": "Cinima-city Ramat-hashron",
"Amount": 15,
"Date": "15/5/18"
},
"Transaction_3": {
"Name": "Superfarm",
"Location": "Shivat-hacochvim male",
"Amount": 199,
"Date": "7/5/18"
},
"Transaction_4": {
"Name": "Printing solutions",
"Location": "Afeka tel-aviv",
"Amount": 16,
"Date": "25/5/18"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With