[英]How to get value from JSON - Python
If anyone can help me to get a value from a json response.如果有人可以帮助我从 json 响应中获取值。 I intend to get only the first incidence of id where the type is CLIENTREF.
我打算只获取类型为 CLIENTREF 的 id 的第一次出现。 I am reading a list to get some values to do a GET and check if the response is other than null "".
我正在阅读一个列表以获取一些值来执行 GET 并检查响应是否不是空“”。
This is the JSON structure:这是 JSON 结构:
[{
'_xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'_xsi:noNamespaceSchemaLocation': 'http://svc-bdmentity01p:8080/schema/12.0.0/com/mizuho/bdm/cmf/cmfCounterparty.xsd',
'vendor': 'goldenSource',
'customerType': 13,
'partyType': 'counterparty',
'customerName': 'DEUTSCHE BANK AG ACTING AS IRS CLEARING BROKER (LCH)',
'shortName': 'DEU BNK LCH - SUB GROUP MARKER',
'bloombergCompanyName': 'LCH Ltd',
'customerStatus': 'ACTIVE',
'parent': {
'ultimateParentCompanyID': 8198675,
'ultimateParentName': 'LCH Group Holdings Ltd',
'ultimateParentCntryOfDomicile': 'GB',
'ultimateParentCntryOfIncorporation': 'GB',
'ultimateParentCntryOfRisk': 'GB',
'ultimateParentAddressLine1': '33 Aldgate High Street',
'ultimateParentAddressLine2': '33 Aldgate High Street',
'ultimateParentAddressLine3': 'London',
'ultimateParentAddressLine4': 'United Kingdom'
},
'riskUltimateParent': {
'name': 'LCH Group Holdings Ltd',
'identifier': [{
'type': 'BBCMPYID',
'id': '8198675'
}, {
'type': 'CLIENTREF',
'id': 'LCHCLRNTA'
}, {
'type': 'SECOREFE',
'id': 'LCHCLRNETG'
}]
},
'riskImmediateParent': {
'name': 'LCH Group Holdings Ltd',
'identifier': [{
'type': 'BBCMPYID',
'id': '8198675'
}, {
'type': 'CLIENTREF',
'id': 'LCHCLRNTA'
}]
},
'identifiers': {
'identifier': [{
'custId': 'DEUBLCHBSG',
'custIdType': 'STRATREF'
}, {
'custId': '20675',
'custIdType': 'CUSTREF'
}, {
'custId': '932835',
'custIdType': 'BBCMPYID'
}, {
'custId': 'F226TOH6YD6XJB17KS62',
'custIdType': 'LEI'
}, {
'custId': 'LCH ISSUER',
'custIdType': 'CLIENTREF'
}, {
'custId': 'LCHCLRNETG',
'custIdType': 'GROUPREF'
}]
},
'location': 'Frankfurt',
'cntryOfIncorporation': 'GB',
'cntryOfRisk': 'GB',
'cntryOfResidence': 'DE',
'legalStatus': 'AG - Aktiengesellschaft-stock corporation',
'lchMember': 'Y',
'headOfficeCategory': 1,
'registeredAddressLine1': 'TAUNUSANLAGE 12',
'registeredAddressCityName': 'FRANKFURT AM MAIN',
'registeredAddressPostalCode': '60325',
'registeredAddressCountry': 'DE',
'registeredAddressCountryName': 'Germany',
'registeredTelephoneNumber': '00496991000',
'registeredFaxNumber': '00496991034225',
'clientCreatedTime': '2019-03-27T13:17:45.000Z',
'clientLastUpdatedTime': '2021-09-28T06:15:21.000Z',
'nextLimitReviewDate': '2020-03-27T00:00:00.000Z',
'nextReviewDate': '2022-03-28T00:00:00.000Z',
'principalAcctOfficer': [{
'#value': 'krafpe',
'_organisationId': 'MHEU'
}],
'stratus': {
'principalAgent': 'Principal',
'supraNational': 'N'
},
'classifications': {
'customerType': 'STANDARD',
'regulatoryClass': 'PC1',
'amlCategory': 'A',
'bOfECode': '4320000730',
'gicsLevel': '4',
'gicsCode': '40M51010',
'internationalOrgCode': '9',
'classification': [{
'type': 'InstitutionClearingExchangeCategory',
'id': 'Clearing'
}]
},
'fatcaDetails': {
'usClient': 'N'
},
'emirDetails': {
'clientType': 'FC'
},
'mifidDetails': {
'powerOfRepresentation': 'N',
'assistedReporting': 'N',
'clientIsPrivateIndividual': 'Y',
'clientVATNumber': 'DE114103379'
},
'ratings': {
'ecai': 'AA-',
'ecaiStandard': 'AA-',
'ecaiVendor': 'SAP',
'sAndPRating': 'AA-',
'sAndPRatingSetDescription': 'Local Currency LT',
'internal': 'A+',
'derivedPartyPolicy': ['A+']
},
'custLimits': {
'limit': [{
'limitType': 'PEAEXPLI',
'limitAmount': 0,
'limitCurrency': 'GBP'
}, {
'limitType': 'PEAEXPLI',
'limitAmount': 0,
'limitCurrency': 'GBP'
}, {
'limitType': 'DOPELIMI',
'limitAmount': 0,
'limitCurrency': 'USD'
}, {
'limitType': 'DIPELIMI',
'limitAmount': 0,
'limitCurrency': 'USD'
}]
},
'flag': [{
'#value': 'N',
'_TYPE': 'automatedReconciliation'
}],
'accounts': {
'account': [{
'organisationId': {
'#value': 'MHEU'
},
'accountName': 'DEUTSCHE BANK AG (LCH)',
'identifiers': {
'accountId': 'LCHSWPDEUT',
'customerId': 'DEUBLCHBSG',
'blockAccountCode': 'LCHSWPDEUT',
'identifier': [{
'accountId': 'MHI',
'accountIdType': 'REVNCNTR'
}, {
'accountId': 'LCHSWPDEUT',
'accountIdType': 'ACCOUNTID'
}, {
'accountId': 'DEUBLCHBSG',
'accountIdType': 'MHICUSTID'
}, {
'accountId': 'LCHSWPDEUT',
'accountIdType': 'BLOCKACCOUNT'
}, {
'accountId': 'LCHSWPDEUT',
'accountIdType': 'GLOBEOP'
}]
},
'isBlocAccount': 'N',
'accountStatus': 'COMPLETE',
'products': {
'productType': [{
'productLineName': 'DERIVATIVES',
'productTypeId': 'PRODMH24',
'productTypeName': 'Overnight Index Swaps',
'limitQualifier': 'ISDA/FIA ECDEA',
'cleared': 'Y',
'bilateral': 'N',
'limitInstructions': {
'limitInstruction': [{
'limitAmount': '0',
'limitCurrency': 'GBP',
'limitType': 'PEAEXPLI',
'limitTypeName': 'Cash-Peak Exposure Limit'
}]
}
}, {
'productLineName': 'DERIVATIVES',
'productTypeId': 'PRODMH21',
'productTypeName': 'Interest Rate Swaps',
'limitQualifier': 'ISDA/FIA ECDEA',
'cleared': 'Y',
'bilateral': 'N',
'limitInstructions': {
'limitInstruction': [{
'limitAmount': '0',
'limitCurrency': 'GBP',
'limitType': 'PEAEXPLI',
'limitTypeName': 'Cash-Peak Exposure Limit'
}]
}
}]
},
'costCentre': 'European Rates',
'clientLevel': 'SUBAC',
'accountCreationDate': '2019-03-29T00:00:00.000Z',
'accountOpeningDate': '2019-03-29T00:00:00.000Z',
'cntryOfDomicile': 'DE'
}, {
'organisationId': {
'#value': 'MHEU'
},
'accountName': 'DEUTSCHE BANK (LCH) - SWAPS',
'identifiers': {
'accountId': 'DEUTBANSWP',
'customerId': 'DEUBLCHBSG',
'blockAccountCode': 'DEUTBANSWP',
'identifier': [{
'accountId': 'MHI',
'accountIdType': 'REVNCNTR'
}, {
'accountId': 'DEUTBANSWP',
'accountIdType': 'ACCOUNTID'
}, {
'accountId': 'DEUBLCHBSG',
'accountIdType': 'MHICUSTID'
}, {
'accountId': 'DEUTBANSWP',
'accountIdType': 'BLOCKACCOUNT'
}, {
'accountId': 'DEUTBANSWP',
'accountIdType': 'GLOBEOP'
}]
},
'isBlocAccount': 'N',
'accountStatus': 'CLSED',
'products': {
'productType': [{
'productLineName': 'DERIVATIVES',
'productTypeId': 'PRODMH21',
'productTypeName': 'Interest Rate Swaps',
'limitQualifier': 'ISDA/FIA ECDEA',
'cleared': 'Y',
'bilateral': 'N',
'limitInstructions': {
'limitInstruction': [{
'limitAmount': '0',
'limitCurrency': 'GBP',
'limitType': 'PEAEXPLI',
'limitTypeName': 'Cash-Peak Exposure Limit'
}]
}
}, {
'productLineName': 'DERIVATIVES',
'productTypeId': 'PRODMH24',
'productTypeName': 'Overnight Index Swaps',
'limitQualifier': 'ISDA/FIA ECDEA',
'cleared': 'Y',
'bilateral': 'N',
'limitInstructions': {
'limitInstruction': [{
'limitAmount': '0',
'limitCurrency': 'GBP',
'limitType': 'PEAEXPLI',
'limitTypeName': 'Cash-Peak Exposure Limit'
}]
}
}]
},
'costCentre': 'Rate Sales',
'accountClosed': '2019-03-29T17:35:25.000',
'clientLevel': 'SUBAC',
'accountCreationDate': '2019-03-28T00:00:00.000Z',
'accountOpeningDate': '2019-03-29T00:00:00.000Z',
'cntryOfDomicile': 'DE'
}, {
'organisationId': {
'#value': 'MHEU'
},
'accountName': 'DEUTSCHE BANK (LCH) PRE-FUNDING',
'identifiers': {
'accountId': 'DBPRFNDLCH',
'customerId': 'DEUBLCHBSG',
'blockAccountCode': 'DBPRFNDLCH',
'identifier': [{
'accountId': 'MHI',
'accountIdType': 'REVNCNTR'
}, {
'accountId': 'DBPRFNDLCH',
'accountIdType': 'ACCOUNTID'
}, {
'accountId': 'DEUBLCHBSG',
'accountIdType': 'MHICUSTID'
}, {
'accountId': 'DBPRFNDLCH',
'accountIdType': 'BLOCKACCOUNT'
}, {
'accountId': 'DBPRFNDLCH',
'accountIdType': 'GLOBEOP'
}]
},
'isBlocAccount': 'N',
'accountStatus': 'COMPLETE',
'products': {
'productType': [{
'productLineName': 'DERIVATIVES',
'productTypeId': 'PRODMH24',
'productTypeName': 'Overnight Index Swaps',
'limitQualifier': 'ISDA/FIA ECDEA',
'cleared': 'Y',
'bilateral': 'N',
'limitInstructions': {
'limitInstruction': [{
'limitAmount': '0',
'limitCurrency': 'GBP',
'limitType': 'PEAEXPLI',
'limitTypeName': 'Cash-Peak Exposure Limit'
}]
}
}, {
'productLineName': 'DERIVATIVES',
'productTypeId': 'PRODMH21',
'productTypeName': 'Interest Rate Swaps',
'limitQualifier': 'ISDA/FIA ECDEA',
'cleared': 'Y',
'bilateral': 'N',
'limitInstructions': {
'limitInstruction': [{
'limitAmount': '0',
'limitCurrency': 'GBP',
'limitType': 'PEAEXPLI',
'limitTypeName': 'Cash-Peak Exposure Limit'
}]
}
}]
},
'costCentre': 'Rate Sales',
'clientLevel': 'SUBAC',
'accountCreationDate': '2019-03-28T00:00:00.000Z',
'accountOpeningDate': '2019-03-28T00:00:00.000Z',
'cntryOfDomicile': 'DE'
}, {
'organisationId': {
'#value': 'MHEU'
},
'accountName': 'DEUTSCHE BANK (LCH) INITIAL MARGIN',
'identifiers': {
'accountId': 'DBEULCHIM',
'customerId': 'DEUBLCHBSG',
'blockAccountCode': 'DBEULCHIM',
'identifier': [{
'accountId': 'MHI',
'accountIdType': 'REVNCNTR'
}, {
'accountId': 'DBEULCHIM',
'accountIdType': 'ACCOUNTID'
}, {
'accountId': 'DEUBLCHBSG',
'accountIdType': 'MHICUSTID'
}, {
'accountId': 'DBEULCHIM',
'accountIdType': 'BLOCKACCOUNT'
}, {
'accountId': 'DBEULCHIM',
'accountIdType': 'GLOBEOP'
}]
},
'isBlocAccount': 'N',
'accountStatus': 'COMPLETE',
'products': {
'productType': [{
'productLineName': 'DERIVATIVES',
'productTypeId': 'PRODMH24',
'productTypeName': 'Overnight Index Swaps',
'limitQualifier': 'ISDA/FIA ECDEA',
'cleared': 'Y',
'bilateral': 'N',
'limitInstructions': {
'limitInstruction': [{
'limitAmount': '0',
'limitCurrency': 'GBP',
'limitType': 'PEAEXPLI',
'limitTypeName': 'Cash-Peak Exposure Limit'
}]
}
}, {
'productLineName': 'DERIVATIVES',
'productTypeId': 'PRODMH21',
'productTypeName': 'Interest Rate Swaps',
'limitQualifier': 'ISDA/FIA ECDEA',
'cleared': 'Y',
'bilateral': 'N',
'limitInstructions': {
'limitInstruction': [{
'limitAmount': '0',
'limitCurrency': 'GBP',
'limitType': 'PEAEXPLI',
'limitTypeName': 'Cash-Peak Exposure Limit'
}]
}
}]
},
'costCentre': 'Rate Sales',
'clientLevel': 'SUBAC',
'accountCreationDate': '2019-03-28T00:00:00.000Z',
'accountOpeningDate': '2019-03-28T00:00:00.000Z',
'cntryOfDomicile': 'DE'
}, {
'organisationId': {
'#value': 'MHEU'
},
'accountName': 'DEUTSCHE BANK (LCH) VARIATION MARGIN',
'identifiers': {
'accountId': 'DBEULCHVM',
'customerId': 'DEUBLCHBSG',
'blockAccountCode': 'DBEULCHVM',
'identifier': [{
'accountId': 'MHI',
'accountIdType': 'REVNCNTR'
}, {
'accountId': 'DBEULCHVM',
'accountIdType': 'ACCOUNTID'
}, {
'accountId': 'DEUBLCHBSG',
'accountIdType': 'MHICUSTID'
}, {
'accountId': 'DBEULCHVM',
'accountIdType': 'BLOCKACCOUNT'
}, {
'accountId': 'DBEULCHVM',
'accountIdType': 'GLOBEOP'
}]
},
'isBlocAccount': 'N',
'accountStatus': 'COMPLETE',
'products': {
'productType': [{
'productLineName': 'DERIVATIVES',
'productTypeId': 'PRODMH24',
'productTypeName': 'Overnight Index Swaps',
'limitQualifier': 'ISDA/FIA ECDEA',
'cleared': 'Y',
'bilateral': 'N',
'limitInstructions': {
'limitInstruction': [{
'limitAmount': '0',
'limitCurrency': 'GBP',
'limitType': 'PEAEXPLI',
'limitTypeName': 'Cash-Peak Exposure Limit'
}]
}
}, {
'productLineName': 'DERIVATIVES',
'productTypeId': 'PRODMH21',
'productTypeName': 'Interest Rate Swaps',
'limitQualifier': 'ISDA/FIA ECDEA',
'cleared': 'Y',
'bilateral': 'N',
'limitInstructions': {
'limitInstruction': [{
'limitAmount': '0',
'limitCurrency': 'GBP',
'limitType': 'PEAEXPLI',
'limitTypeName': 'Cash-Peak Exposure Limit'
}]
}
}]
},
'costCentre': 'Rate Sales',
'clientLevel': 'SUBAC',
'accountCreationDate': '2019-03-28T00:00:00.000Z',
'accountOpeningDate': '2019-03-28T00:00:00.000Z',
'cntryOfDomicile': 'DE'
}]
},
'counterPartyLastUpdated': '2021-09-28T06:15:21.000Z',
'priceRounding': {
'precision': 4
},
'asOf': '2021-09-28T06:21:54.582Z',
'validation': [{
'#value': '20',
'_validationScheme': 'mhi:hash-value'
}],
'termsOfBusinessDate': None
}]
This is my code so far到目前为止,这是我的代码
def checkParent(list):
for item in list:
cdwParenturl = f"http://cdwu/cdw/counterparties/{item}/?yyyy-mm-dd={dateinplay}"
r = requests.get(cdwParenturl).json()
jsonpath_expression = parse("$..identifier[*]")
# $..identifier[?(@.type == 'CLIENTREF')].id
parentId = []
for match in jsonpath_expression.find(r):
# print(f"match id: {match.value}")
thisdict = match.value
if thisdict["type"] == "CLIENTREF":
# print(thisdict["id"])
parentId.append(thisdict["id"])
elif thisdict["id"] == "":
print(colored(f"The parent {item} does not have ultimateParent", "red"))
print(colored(f"All counterparties have parent", "green"))
checkParent(r_mheu_trade)
print(f "match id: {match.value}") I get this, What I need is the first id related to the type: CLIENTREF print(f "match id: {match.value}") 我明白了,我需要的是与类型相关的第一个 id:CLIENTREF
match id: {'type': 'BBCMPYID', 'id': '8198675'}
match id: {'type': 'CLIENTREF', 'id': 'LCHCLRNTA'}
match id: {'type': 'SECOREFE', 'id': 'LCHCLRNETG'}
match id: {'type': 'BBCMPYID', 'id': '8198675'}
match id: {'type': 'CLIENTREF', 'id': 'LCHCLRNTA'}
The error that appears on my console is this:我的控制台上出现的错误是这样的:
Traceback (most recent call last):
File "h:\DESKTOP\test_check\checkCounterpartie.py", line 114, in <module>
checkParent(r_mheu_trade)
File "h:\DESKTOP\test_check\checkCounterpartie.py", line 106, in checkParent
if thisdict["type"] == "CLIENTREF":
KeyError: 'type'
With the help of pyzer I found a solution to my problem.在 pyzer 的帮助下,我找到了解决问题的方法。 This is how the code looks like:
这是代码的样子:
# Request to get all UltimateParent
def checkParent(murex):
for item in murex:
cdwParenturl = f"http://cdwu/cdw/counterparties/{item}/?yyyy-mm-dd={dateinplay}"
r = requests.get(cdwParenturl).json()
clientref = r[0]["riskUltimateParent"]["identifier"][1]
if clientref == "":
print(colored(f"The parent {item} does not have ultimateParent", "red"))
else:
print(colored(f"All counterparties have parent", "green"))
checkParent(r_mheu_trade)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.