Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

Tags:

python

json

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)

like image 892
Sarel Amrani Avatar asked May 20 '18 04:05

Sarel Amrani


1 Answers

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"
    }
}
like image 103
PM 2Ring Avatar answered Oct 24 '22 18:10

PM 2Ring