0

Im trying to use a json file to use as an arya so on line itsnt extreemely long.

Code in Python I am using atm to try to read the JSON thing: (I found some other thing in another question and it got me this far)

import json

with open('rooms.json') as f:
    rooms = json.load(f)

The code in rooms.json:

[
// The Skeld 0-13
"Upper Engine", 
"Cafeteria", 
"Weapons",
"Reactor",
"Security",
"Medbay",
"O2",
"Navigation",
"Lower Engine",
"Electrical",
"Storage",
"Admin",
"Communications",
"Shields",

// Polus 14-29
"Northwest",
"Northeast",
"Laboratory",
"Security",
"Electrical",
"Storage",
"O2",
"Communications",
"Central",
"Office",
"East",
"Southwest",
"Weapons",
"South",
"Admin",
"Specimen Room",

//MIRA HQ 29-41
"Greenhouse",
"Office",
"Admin",
"Reactor",
"Laboratory",
"launchpad",
"Locker room",
"Communicatins",
"Medbay",
"torage",
"afeteria'"
"balcony"
]

The error i get (i use sublime btw and python 3.8.6):

Traceback (most recent call last):
  File "C:\src\Python\AmongUs\AmongUs.py", line 7, in <module>
    rooms = json.load(f)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)
[Finished in 0.8s]

Image of the thing in Python: Immage

Martin
  • 75
  • 8
  • 1
    Unfortunately your `rooms.json` file is not `json`. Where did you get this file from? Can you persuade who ever produced it to produce valid `json`? – quamrana Oct 08 '20 at 19:50
  • Are the names in the comments meant to group the lists below them? – tdelaney Oct 08 '20 at 19:53
  • 1
    @buran - that's potentially a great link but it depends on whether the information in the comments is something OP wants to keep. It could be that we have data for The Skeld, then for Polus, etc... OP doesn't seem to want to clarify that so we just have to wait. – tdelaney Oct 08 '20 at 20:03
  • @tdelaney, agree. Although, if that information is important, it really shouldn't be a comment – buran Oct 08 '20 at 20:08
  • when i remove comment i still get error (changed some stuff too) https://pastebin.com/GE7JmzbP – Martin Oct 08 '20 at 20:10
  • You have a different program there. Your code from your original post works when you remove the comments from `rooms.json` – quamrana Oct 08 '20 at 20:16
  • i fixed it ajsdhajkdhajkdhjkadhjkahdjkahdj – Martin Oct 08 '20 at 20:25

2 Answers2

1

You can use the commentjson module to parse json with embedded comments.

pip install commentjson

Note:: Your string has invalid json even if you ignore the comments! In the very end:

"torage",
"afeteria'",
"balcony"
]

There's an extra apostrophe in "afeteria'" that isn't escaped, and a missing comma at the end of the same line.

import commentjson as jsonc

jsonc_string = """[
// The Skeld 0-13
"Upper Engine", 
"Cafeteria", 
"Weapons",
"Reactor",
"Security",
"Medbay",
"O2",
"Navigation",
"Lower Engine",
"Electrical",
"Storage",
"Admin",
"Communications",
"Shields",

// Polus 14-29
"Northwest",
"Northeast",
"Laboratory",
"Security",
"Electrical",
"Storage",
"O2",
"Communications",
"Central",
"Office",
"East",
"Southwest",
"Weapons",
"South",
"Admin",
"Specimen Room",

//MIRA HQ 29-41
"Greenhouse",
"Office",
"Admin",
"Reactor",
"Laboratory",
"launchpad",
"Locker room",
"Communicatins",
"Medbay",
"torage",
"Cafeteria",
"balcony"
]"""

lst = jsonc.loads(jsonc_string)

And this gives us the parsed list:

print(lst)
# Output: 
# ['Upper Engine', 'Cafeteria', 'Weapons', 'Reactor', 'Security', 'Medbay', 'O2', 'Navigation', 'Lower Engine', 'Electrical', 'Storage', 'Admin', 'Communications', 'Shields', 'Northwest', 'Northeast', 'Laboratory', 'Security', 'Electrical', 'Storage', 'O2', 'Communications', 'Central', 'Office', 'East', 'Southwest', 'Weapons', 'South', 'Admin', 'Specimen Room', 'Greenhouse', 'Office', 'Admin', 'Reactor', 'Laboratory', 'launchpad', 'Locker room', 'Communicatins', 'Medbay', 'torage', 'Cafeteria', 'balcony']
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
0

The problem is that JSON does not support comments

$ python -c 'import json; json.loads("""[
> // here is a comment
> "some string"]""")'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/michaelsmith/.pyenv/versions/3.8.3/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/Users/michaelsmith/.pyenv/versions/3.8.3/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/michaelsmith/.pyenv/versions/3.8.3/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 2)

You either must remove the comments and produce valid json, or use a format that does support comments. Since YAML is a superset of json, you could parse these files as yaml, only changing the comments to start with # instead of //.

Other formats that support comments are ini (via python's configparser module) and toml.

kojiro
  • 74,557
  • 19
  • 143
  • 201