0
votes

I currently have a list of files. I am aiming to sort the data into separate arrays.

The data is similar to the following content repeated many times.

    [
    {
        "private": {
            "bankroll": 1000,
            "last_bid": 0,
            "launching": false,
            "name": "abc",
            "tech": 0
        },
        "public": {
            "auction_round": 0,
            "game_stage": "start",
            "last_mining_payoff": 0,
            "last_winning_bid": 0,
            "last_winning_bidders": [],
            "last_winning_miner": "",
            "max_rounds": 201,
            "players": {
                "AggressiveLauncher": {
                    "bankroll": 1000
                },
                "abc": {
                    "bankroll": 1000
                },
                "PassiveLauncher": {
                    "bankroll": 1000
                },
                "def": {
                    "bankroll": 1000
                },
                "SpongeBob": {
                    "bankroll": 1000
                }
            },
            "round": 0
        }
    }, ...]

Code snippet,

    for a in range(len(abc_files)):
        temp=str(abc_files[a])
        path='designated path'
        print(path)
        with open(path, "r") as b:
            lines = json.load(b)
            lines=str(lines)
            lines=lines.replace("\'","\"")
            print(lines)
            list_temp=json.loads(lines)
            for i in range(len(list_temp)):
                bankroll.append(list_temp[i]['private']['bankroll'])
                print(bankroll)

Whenever it runs, I am currently generating an error for the list_temp=json.loads(lines) line. The error is as follows list_temp=json.loads(lines)

File "C:\Users\abc\anaconda3\lib\json_init_.py", line 357, in loads return _default_decoder.decode(s)

File "C:\Users\abc\anaconda3\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end())

File "C:\Users\abc\anaconda3\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None JSONDecodeError: Expecting value

The data is already as a string too, it was the first thing I attempted.

1
Hve you double checked that there are no errors in your json file? - Hampus
Share the code around list_temp=json.loads(lines) - bherbruck
The only other part of code in the file is this "abc_files = os.listdir("abc/")" which just gets the data from the file. the rest is already printed - Hamzah
I have double checked however I'm not very knowledgeable when it comes to json so may have missed something - Hamzah

1 Answers

0
votes

You are trying to replace the single quote char with the double quotes char when it's not needed, in the example that you provided there are no single quotes. Also it's worth noting that single quotes are not allowed in JSON.

When you use json.load() a list containing dictionaries is returned, so there's no need to add the steps you had in your code.

You can try with the following,

with open(path, "r") as b:
    lines = json.load(b)
    print(type(lines))
    for i in range(len(lines)):
        bankroll.append(lines[i]['private']['bankroll'])

Unless that you are try to acomplish something else, it should work.

Also theres a clearer way of doing the same thing asuming you will have a list of dictionaries would be

with open(path, "r") as f:
    lines = json.load(f)
    for data in lines:
        bankroll.append(data['private']['bankroll'])

Instead of using range you could iter through the list objects, named data in this case just for the purpose of the example. This way the code is much more readable, and it's good when you work with a lot of nested data structures to keep track of what you are doing.