How can I extract just one value from a json output?

I am using python requests to retrieve data from an API and I need to filter out just one value.

I am new to JSON and I hope someone could give me a hand.

Below is my code and the returned output. However, I only need the very first case of “expiryDate” under “motTests” which in this case is “2022.04.05”. How can I filter this out? Thanks in advance.

My code:

import requests

url = "https://beta.check-mot.service.gov.uk/trade/vehicles/mot-tests"
headers = {
    'Accept': 'application/json+v6',
    'x-api-key': 'my_api_key'
}

resp = requests.get(url, headers=headers, params={'registration': 'YD14YCP'}
)

print(resp.content)

My received output is:

b'[{"registration":"YD14YCP","make":"BMW","model":"530","firstUsedDate":"2014.03.01","fuelType":"Diesel","primaryColour":"White","vehicleId":"YRVs92Gaddsa65jq2zD2Q==","registrationDate":"2014.03.01","manufactureDate":"2014.03.01","engineSize":"2993","motTests":[{"completedDate":"2021.03.25 14:09:25","testResult":"PASSED",**"expiryDate":"2022.04.05"**,"odometerValue":"63823","odometerUnit":"mi","motTestNumber":"5570263656619","odometerResultType":"READ","rfrAndComments":[]},{"completedDate":"2020.03.20 10:30:55","testResult":"PASSED","expiryDate":"2021.04.05","odometerValue":"57548","odometerUnit":"mi","motTestNumber":"8165169314","odometerResultType":"READ","rfrAndComm
    ents":[{"text":"Rear Brake pad(s) wearing thin (1.1.13 (a) (ii))","type":"ADVISORY","dangerous":false},{"text":"Front Tyre worn close t
    o legal limit/worn on edge  both slightly worn on outer edge (5.2.3 (e))","type":"ADVISORY","dangerous":false}]},{"completedDate":"2019
    .04.06 09:34:32","testResult":"PASSED","expiryDate":"2020.04.05","odometerValue":"47558","odometerUnit":"mi","motTestNumber":"403589926
    352","odometerResultType":"READ","rfrAndComments":[]},{"completedDate":"2018.04.06 08:03:08","testResult":"PASSED","expiryDate":"2019.04.05","odometerValue":"41101","odometerUnit":"mi","motTestNumber":"946286448569","odometerResultType":"READ","rfrAndComments":[]},{"com
    pletedDate":"2017.02.28 14:42:55","testResult":"PASSED","expiryDate":"2018.02.28","odometerValue":"28624","odometerUnit":"mi","motTestN
    umber":"37263578588","odometerResultType":"READ","rfrAndComments":[{"text":"Offside Rear Tyre worn close to the legal limit  2-3mm (4.
    1.E.1)","type":"ADVISORY","dangerous":false}]}]}]'

Answer

Try this:

import json
raw = '[{"registration":"YD14YCP","make":"BMW","model":"530","firstUsedDate":"2014.03.01","fuelType":"Diesel","primaryColour":"White","vehicleId":"YRVs92Gaddsa65jq2zD2Q==","registrationDate":"2014.03.01","manufactureDate":"2014.03.01","engineSize":"2993","motTests":[{"completedDate":"2021.03.25 14:09:25","testResult":"PASSED","expiryDate":"2022.04.05","odometerValue":"63823","odometerUnit":"mi","motTestNumber":"5570263656619","odometerResultType":"READ","rfrAndComments":[]},{"completedDate":"2020.03.20 10:30:55","testResult":"PASSED","expiryDate":"2021.04.05","odometerValue":"57548","odometerUnit":"mi","motTestNumber":"8165169314","odometerResultType":"READ","rfrAndComm ents":[{"text":"Rear Brake pad(s) wearing thin (1.1.13 (a) (ii))","type":"ADVISORY","dangerous":false},{"text":"Front Tyre worn close t o legal limit/worn on edge both slightly worn on outer edge (5.2.3 (e))","type":"ADVISORY","dangerous":false}]},{"completedDate":"2019 .04.06 09:34:32","testResult":"PASSED","expiryDate":"2020.04.05","odometerValue":"47558","odometerUnit":"mi","motTestNumber":"403589926 352","odometerResultType":"READ","rfrAndComments":[]},{"completedDate":"2018.04.06 08:03:08","testResult":"PASSED","expiryDate":"2019.04.05","odometerValue":"41101","odometerUnit":"mi","motTestNumber":"946286448569","odometerResultType":"READ","rfrAndComments":[]},{"com pletedDate":"2017.02.28 14:42:55","testResult":"PASSED","expiryDate":"2018.02.28","odometerValue":"28624","odometerUnit":"mi","motTestN umber":"37263578588","odometerResultType":"READ","rfrAndComments":[{"text":"Offside Rear Tyre worn close to the legal limit 2-3mm (4. 1.E.1)","type":"ADVISORY","dangerous":false}]}]}]'

res = json.loads(response)
print(res[0]["motTests"][0]["expiryDate"])

[OUT]

'2022.04.05'