CharmiChokshi
CharmiChokshi

Reputation: 3

How to get file "Path" stored in this json file using Python?

json file

[{"Attachment": [
{
    "Page:": [
    {
        "Path": "a\\b\\c.pdf",  #field to be extracted
        "PageID": 1
    }
    ]
}
],
"ID": 13221}]

I tried the following but getting the TypeError: list indices must be integers, not str

with open(file) as f:
    d = json.load(f)
print(d[0]['Attachment']['Page']['Path'])

Upvotes: 0

Views: 338

Answers (1)

Reupiey
Reupiey

Reputation: 277

d[0]['Attachment'] is a list, so is d[0]['Attachment'][0]['Page:'].

with open(file) as f:
    d = json.load(f)
print(d[0]['Attachment'][0]['Page:'][0]['Path'])

will do the job.

Upvotes: 2

Related Questions