10

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)

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
Sarel Amrani
  • 111
  • 1
  • 1
  • 4
  • 1
    That isn't valid JSON. The doubled braces at the start and end should be single braces, and there's a missing quote after `Landver`. – PM 2Ring May 20 '18 at 04:30
  • Thanks, did it , but it still wont work: obj, end = self.scan_once(s, idx) json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 1 (char 92) – Sarel Amrani May 20 '18 at 04:32
  • Removing the doubled braces at the start isn't sufficient. Either you have to turn it into a list of values (by using square brackets instead of braces), turn it into a flat object (by removing the braces around each line), or… well, there are lots of possible things that are sort of close to this that are valid JSON, but it's hard to guess which of those things you want. – abarnert May 20 '18 at 04:34
  • I made a few other changes (like what abarnert suggest) which I forgot to mention. Sorry about that! I'll post a working version in a minute. – PM 2Ring May 20 '18 at 04:36
  • 2
    The real question is: Where did this file come from? It's not valid JSON, and requires multiple changes—and guessing at what those changes should be—before it's parseable. After which it still may not be what you actually wanted. If you wrote the code that generated this file, you need to fix _that_ code. Or, if you created the file manually in a text editor, you should get a smarter editor that helps you format JSON properly, or use a site like [JSONLint](https://jsonlint.com/). – abarnert May 20 '18 at 04:38
  • can someone write the right one, because the error is repeated. thanks a lot! – Sarel Amrani May 20 '18 at 04:39
  • 2
    What is "the right one"? Is this supposed to be a list of single-element dicts, or a flat dict? And, more importantly: Where did it come from? Some person or some code generated this file. What you're asking us to do is to guess the intention of that person, or the person who wrote that code. You probably know who that person is, or can see that code—or at least you know why you want to use their output—so you can guess better than we can. Or you can tell us what you know. – abarnert May 20 '18 at 04:43
  • I created it on notepad – Sarel Amrani May 20 '18 at 04:49
  • Ok. So now you know why it can be dangerous to edit JSON by hand. ;) – PM 2Ring May 20 '18 at 04:51
  • It is for school project, I just wanted to upload json format to {} dic format. – Sarel Amrani May 20 '18 at 04:51
  • You should always create JSON using a JSON library, not do it by hand. This will ensure that it's in correct format. – Barmar May 20 '18 at 05:35

1 Answers1

9

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"
    }
}
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182