0
urls = ['http://google.com', 'http://yandex.com']

jobs = {
    "jobs": [{
        "id": "4535765",
        "urls": [{
            "url": "http://google.com",
            "status": "OK"
        }, {
            "url": "http://yandex.com",
            "status": "OK"
        }]
    }, {
        "id": "4535756",
        "urls": [{
            "url": "http://example.com",
            "status": "OK"
        }, {
            "url": "http://google.it",
            "status": "OK"
        }]
    }]
}

I need to match the job id that has as urls all the urls in urls.

job_id = [j['id'] for j in jobs['jobs'] if all(u in jobs['urls'] for u in urls)]

Of course it doesn't work, cant think a way to interact with every url in urls.

1
  • This is independent of JSON; you just have a Python dict. Commented Mar 21, 2017 at 17:22

1 Answer 1

2

You were really close, you just weren't actually grabbing the url values from the url object within the jobs. Try this:

job_ids = [j['id'] for j in jobs['jobs'] if all(u['url'] in urls for u in j['urls'])]
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.