0

I have a text file that looks like so:

[{
"x": 0.37375009059906006,
"y": 0.858906626701355,
"y": 1.2558532880291295e-08,
},
{
"x": 0.4551462233066559,
"y": 0.8060519695281982,
"y": -0.023612480610609055,
},
{
"x": 0.5198760032653809,
"y": 0.7056148648262024,
"y": -0.0391654446721077,
},
etc, etc

And I want to know how to convert it into a proper list in python which stores the proper dictionaries.

My current code looks like this:

def create_array(type):
    path = f'data/hands/{type}'
    files = os.listdir(path)
    data = []
    for file in files:
        with open(f'{path}/{file}', 'r') as f:
            c_data = json.loads(f.read())
        data.append(c_data)
    data = [dict(x) for x in data]
    print(data)

But I'm just getting this error:

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 5 column 1 (Char 82)

Anyone know how to get it working?

DirpyToes
  • 3
  • 2
  • [Here](https://stackoverflow.com/questions/39491420/python-jsonexpecting-property-name-enclosed-in-double-quotes) are a lot of answers, and solutions (even in the comments). I would suggest to take a look at them. (As we can't see the whole text, there is no way to tell if there is a comma at the end and so on) – Franciska Feb 08 '23 at 10:28
  • Briefly this tells you there's something wrong with one or more syntax errors in your json files that you're attempting to load. Avoid using duplicate keys `y` too. – GIZ Feb 08 '23 at 10:29
  • Replace `json.loads` with `yaml.loads` and it should just work without having to edit the file. Note: You have to add pyyaml dependency – balki Feb 08 '23 at 15:54

1 Answers1

0

As you are reading the file as JSON, you are getting an exception due to the , at the end of the last line on each dictionary (the second "y" value), so you have to remove these last extra commas. Like this:

[
    {
    "x": 0.37375009059906006,
    "y": 0.858906626701355,
    "y": 1.2558532880291295e-08
    },
    {
    "x": 0.4551462233066559,
    "y": 0.8060519695281982,
    "y": -0.023612480610609055
    },
    {
    "x": 0.5198760032653809,
    "y": 0.7056148648262024,
    "y": -0.0391654446721077
    }
]

Also, bear in mind that you are storing this data in dictionaries, so you should avoid repeating keys, otherwise, the duplicated ones will take the last value read. So I strongly recommend changing the last "y" on each dictionary to another key, e.g.: "z".

Finally, your list data is not necessary as the json.loads() will return a list of dictionaries. Look at this simplified version of your code:

def create_array(path):
    with open(path, 'r') as f:
        c_data = json.loads(f.read())
    print(c_data)

This outputs:

[{'x': 0.37375009059906006, 'y': 1.2558532880291295e-08}, {'x': 0.4551462233066559, 'y': -0.023612480610609055}, {'x': 0.5198760032653809, 'y': -0.0391654446721077}]

Note how the values 0.858906626701355, 0.8060519695281982, and 0.7056148648262024 were replaced by 1.2558532880291295e-08, -0.023612480610609055, and -0.0391654446721077 respectively because of the duplicated key "y".

CreepyRaccoon
  • 826
  • 1
  • 9
  • 19