Iterate through a JSON Object in Python

Posted under » Python on 21 Mar 2025

For PHP Decoding JSON we use the jsonIterator function. In Python, one of the way is to use the jsonpath-ng library. Let us use the same JSON we used previously.

{
  "FIFA_World_Cup_finals": [
    {
      "Year": "2018",
      "data": {
        "Winner": "France",
        "Score": "4-2",
        "Runner-up": "Croatia"
      }
    },
    {
      "Year": "2014",
      "data": {
        "Winner": "Germany",
        "Score": "1-0",
        "Runner-up": "Argentina"
      }
    },
    {
      "Year": "2010",
      "data": {
        "Winner": "Spain",
        "Score": "1-0",
        "Runner-up": "Netherlands"
      }
    }
  ]
}

Assuming the above = response

from jsonpath_ng import jsonpath, parse
import json

json_data = json.loads(response)

expression = parse('FIFA_World_Cup_finals[*].Year')

matches = [match.value for match in expression.find(json_data)]

for match in matches:
    print(match)

We can see that the expression parser is like a regex. And the key in this example is Year and because it is a wildcard *, it will show all the 3 years.

If you want to get France, then your expression is

expression = parse('FIFA_World_Cup_finals[0].data.Winner')

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data