0
folders_list = {
   "folders": [{
   "id": "866699",
   "name": "Folder1",
    "files_count": 0,
    "size": "0",
    "has_sub": false
    }, {
    "id": "866697",
    "name": "Folder2",
    "files_count": 0,
    "size": "0",
    "has_sub": false
  }]

I need to get the folder id knowing the folder name. I thought that I could convert the json into a python dictionary and do

folder_id = [f['id'] for f in folders_list if f['name'] == 'Folder2'][0]

But Python doesn't let me convert it into a dictionary because it doesn't recognize "true" and "false" values.

1 Answer 1

1

The json library can handle false and true in the data:

import json

json_str = """
{
   "folders": [{
   "id": "866699",
   "name": "Folder1",
    "files_count": 0,
    "size": "0",
    "has_sub": false
    }, {
    "id": "866697",
    "name": "Folder2",
    "files_count": 0,
    "size": "0",
    "has_sub": false
  }]
}
"""

data = json.loads(json_str)
folders_list = data['folders']
folder_id = [f['id'] for f in folders_list if f['name'] == 'Folder2'][0]
print "folder_id = %s" % folder_id

Output

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.