0

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.

2 Answers 2

1

Try loading JSON first.

Sample output:

enter image description here

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"])
Sign up to request clarification or add additional context in comments.

Comments

0

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://']

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.