Given this json string, how can I pull out the value of id
if code
equals 4003?
error_json = '''{
'error': {
'meta': {
'code': 4003,
'message': 'Tracking already exists.',
'type': 'BadRequest'
},
'data': {
'tracking': {
'slug': 'fedex',
'tracking_number': '442783308929',
'id': '5b59ea69a9335baf0b5befcf',
'created_at': '2018-07-26T15:36:09+00:00'
}
}
}
}'''
I can't assume that anything other than the error
element exists at the beginning, so the meta
and code
elements may or may not be there. The data
, tracking
, and id
may or may not be there either.
The question is how to extract the value of id
if the elements are all there. If any of the elements are missing, the value of id
should be None