Extracting Values from a nested JSON
Extracting Values from a nested JSON
(OP)
Hi,
I'd like to extract certain values from an API call which is returning a nested JSON file.
This is some of the Python I use to call the API:
Current API Call
classification = client.classifyProduct(Text =text, EndpointArn = endpoint ) #API call for document classification, calling custom model via endpoint
QuereyClassification = ['Name']
QuereyClassicationScore = ['Score']
print(QuereyClassification)
print(QuereyClassicationScore)
return {
'body': str(classification) #body returned from our function
}
Sample output below
{
"body": "{'Classes': [{'Name': 'Loans', 'Score': 0.7328000068664551}, {'Name': 'Mortgage', 'Score': 0.14270000159740448}, {'Name': 'Savings', 'Score': 0.0649000033736229}], 'ResponseMetadata': {'RequestId': 'eb8e3b9f-2116-4398-8af4-e15a7ae609fb', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': 'eb8e3b9f-2116-4398-8af4-e15a7ae609fb', 'content-type': 'application/x-amz-json-1.1', 'content-length': '151', 'date': 'Fri, 06 May 2022 18:36:04 GMT'}, 'RetryAttempts': 0}}"
}
Instead of the above output format, I'd like the result to display the the first entry for Name and Score in the list and store into separate variables, from the above example it would be
VarProduct: Loans
VarScore: 0.7328000068664551
Would be great if someone can point me to some code in Python that would achieve this, TIA
I'd like to extract certain values from an API call which is returning a nested JSON file.
This is some of the Python I use to call the API:
Current API Call
classification = client.classifyProduct(Text =text, EndpointArn = endpoint ) #API call for document classification, calling custom model via endpoint
QuereyClassification = ['Name']
QuereyClassicationScore = ['Score']
print(QuereyClassification)
print(QuereyClassicationScore)
return {
'body': str(classification) #body returned from our function
}
Sample output below
{
"body": "{'Classes': [{'Name': 'Loans', 'Score': 0.7328000068664551}, {'Name': 'Mortgage', 'Score': 0.14270000159740448}, {'Name': 'Savings', 'Score': 0.0649000033736229}], 'ResponseMetadata': {'RequestId': 'eb8e3b9f-2116-4398-8af4-e15a7ae609fb', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': 'eb8e3b9f-2116-4398-8af4-e15a7ae609fb', 'content-type': 'application/x-amz-json-1.1', 'content-length': '151', 'date': 'Fri, 06 May 2022 18:36:04 GMT'}, 'RetryAttempts': 0}}"
}
Instead of the above output format, I'd like the result to display the the first entry for Name and Score in the list and store into separate variables, from the above example it would be
VarProduct: Loans
VarScore: 0.7328000068664551
Would be great if someone can point me to some code in Python that would achieve this, TIA
RE: Extracting Values from a nested JSON
This works for me:
json_parse.py
CODE
Result:
CODE
RE: Extracting Values from a nested JSON
RE: Extracting Values from a nested JSON
You're welcome!