Reputation: 3
The JSON file is like this:
[
{
"src": "https://",
"viewcount": "0"
},
{
"src": "https://",
"viewcount": "0"
}
]
I wish to extract all the values under src
(all of which are urls) with python.
How can I do so?
Thank you.
Upvotes: 0
Views: 246
Reputation: 561
Try loading JSON first.
Sample output:
import json
myjsonstr = '[{"src": "https://google.com","viewcount": "0"},{"src": "https://yahoo.com","viewcount": "0"}]'
myjsonobj = json.loads(myjsonstr)
for eachitem in myjsonobj:
print(eachitem["src"])
Upvotes: 1
Reputation: 422
You're looking for the json
module. loads
will try to parse a string (watch out, there's an extra/missing bracket in what you posted).
After that, it's a regular dict.
import json
source = """[
{
"src": "https://",
"viewcount": "0"
},
{
"src": "https://",
"viewcount": "0"
}
]
"""
urls = [x['src'] for x in json.loads(j)]
['https://', 'https://']
Upvotes: 0