Home > Enterprise >  Python - get a LIST value from field in JSON
Python - get a LIST value from field in JSON

Time:04-17

I have a JSON file containing three fields: 2 are strings and third one is field containing a list of values.

{   "STREAM": "stream",
    "BASIS_STREAM": "basis",
    "PATHS": "[/opt/path1,/opt/path2]"
}

Now I load that JSON

with open('/pathToJsonFile.json', 'r') as f:
    data = json.load(f)

Now I want to get those values.

stream=str(data["STREAM"])
basis=str(data["BASIS_STREAM"])
paths=data["BASE_PATHS"]

The issue is that paths is also threated as String, although I have to use it as a list. I am converting with str function other fields because of the Unicode. Code must be in python2. Thanks a lot!

CodePudding user response:

Say you have a file called data.json with the following contents:

{
   "STREAM": "stream",
   "BASIS_STREAM": "basis",
   "PATHS": "[/opt/path1,/opt/path2]"
}

Maybe you could use enter image description here

It should also work if your PATH has more data as following:

enter image description here

  • Related