Filtering JSON data in Python

Given typical JSON of car models like this:

json_data = """{
"cars": [
{
"manufacturer": "ford",
"model": "mondeo"
},
{
"manufacturer": "vauxhall",
"model": "corsa"
}
]
}"""

To filter matching cars by manufacturer in Python, use:

result = [car for car in json_data['cars'] if(car['manufacturer'] == "ford")]

Discussed in answers here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.