K
K
Kris2015-04-24 11:35:35
Python
Kris, 2015-04-24 11:35:35

How to check for multiple elements in json?

Good!
There is json:

{"employees": {
        "employee": [
            {"id": "1",
                "firstName": "Tom",
                "lastName": "Cruise",
                "photo": "http://cdn2.gossipcenter.com/sites/default/files/imagecache/story_header/photos/tom-cruise-020514sp.jpg" },
            {"id": "2",
                "firstName": "Maria",
                "lastName": "Sharapova",
                "photo": "http://thewallmachine.com/files/1363603040.jpg" },
            {"id": "3",
                "firstName": "James",
                "lastName": "Bond",
                "photo": "http://georgesjournal.files.wordpress.com/2012/02/007_at_50_ge_pierece_brosnan.jpg"} ]}
}

Is there a way to check that "employee" only has: "firstName": "Tom", "firstName": "Maria", "firstName": "James" and nothing else?
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2015-04-24
@kristiks

Loop through "employee" and check that all firstNames are in the list ['Tom', 'Maria', 'James']. Something like this:

firstNames = ["Tom", "Maria", "James"]
for e in emploee:
    if e["firstname"] not in firstNames:
        print(e["firstname"], " - лишний!")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question