0

I have an absolute path to a json file which looks like this:

{
  "name": "PyCharm",
  "version": "2019.2.1",
  "buildNumber": "192.6262.63",
  "productCode": "PC",
  "svgIconPath": "../bin/pycharm.svg",
  "launch": [
    {
      "os": "macOS",
      "launcherPath": "../MacOS/pycharm",
      "javaExecutablePath": "../jbr/Contents/Home/bin/java",
      "vmOptionsFilePath": "../bin/pycharm.vmoptions"
    }
  ]
}

The absolute file looks like this:

data = os.path.abspath("/Applications/PyCharm\ CE.app/Contents/Resources/product-info.json")

I'm trying to access the "version" in the Json file using this :

pycharm_version = data["version"]
print(pycharm_version)

But anytime im running the program im getting an error;

TypeError: string indices must be integers

Does anyone have an idea of how to fix it?

3
  • 1
    You have to load the JSON from the file that the path points to. Commented Mar 11, 2020 at 14:33
  • 1
    data is read as a string. parse it as json with json.loads(data) before trying to get values out of it. Commented Mar 11, 2020 at 14:33
  • abspath doesn't read data from a file; it takes a relative path and returns the equivalent absolute path, based on the current working directory. Commented Mar 11, 2020 at 14:40

2 Answers 2

1

Since I provided the solution to your earlier question it's only right that I expand on what needs to be done.

AS @Thruston suggested you need to load the JSON from the file the path points to.

import json

with open('/Applications/PyCharm\ CE.app/Contents/Resources/product-info.json') as json_file:
    data = json.load(json_file)
    version = data['version']

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer and response again! Im having another issue where it cannot locate my file path which is driving me crazy. It comes up with this error (" FileNotFoundError: [Errno 2] No such file or directory: '/Applications/PyCharm\\ CE.app/Contents/Resources/product-info.json' ") its inserting an extra slash near (PyCharm\) which is causing it to not find the file, any recommendations?
Since you are trying a relative path try removing the leading /? You can also use an absolute path which would set the path from your C directory, e.g. C:\home\sally\statusReport
0

data in your situation contains only path. So data is string not json file. Use json library instead

import json

with open(data) as json_file:
    file= json.load(json_file)
feature = file['version']:

4 Comments

There are some terrible choices of variable names here!
@Thruston Such as? If you are referring to file, that's not used in Python 3 anymore. (Though it's a bit odd to use it to refer to the contents of a file, rather than a file-like object itself.)
I'd hate to start an opinion battle, but to me as a native English speaker, "data" is a misleading name for a file path, and "file" is a confusing name for a dictionary of data. Choosing meaningful names is important even in toy examples here.
I just took the names from the question)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.