Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting Values from a nested JSON 1

Status
Not open for further replies.

mmiah1982

MIS
Nov 6, 2012
16
GB
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

 
Hi mmiah1982,

This works for me:
json_parse.py
Code:
[COLOR=#a020f0]import[/color] json
DBG_INFO = [COLOR=#008b8b]False[/color]

my_dictionary = {
[COLOR=#ff00ff]"[/color][COLOR=#ff00ff]body[/color][COLOR=#ff00ff]"[/color]: [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]{'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}}[/color][COLOR=#ff00ff]"[/color]
}

my_body_string = my_dictionary[[COLOR=#ff00ff]"[/color][COLOR=#ff00ff]body[/color][COLOR=#ff00ff]"[/color]]
[COLOR=#a52a2a][b]if[/b][/color] DBG_INFO: [COLOR=#008b8b]print[/color]([COLOR=#ff00ff]"[/color][COLOR=#ff00ff]my_body_string:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]%s[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color] % my_body_string)

[COLOR=#0000ff]# for valid JSON replace single quotes with double quotes[/color]
my_json_string = my_body_string.replace([COLOR=#ff00ff]"[/color][COLOR=#ff00ff]'[/color][COLOR=#ff00ff]"[/color], [COLOR=#ff00ff]'[/color][COLOR=#ff00ff]"[/color][COLOR=#ff00ff]'[/color]) 
[COLOR=#a52a2a][b]if[/b][/color] DBG_INFO: [COLOR=#008b8b]print[/color]([COLOR=#ff00ff]"[/color][COLOR=#ff00ff]my_json_string:[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]%s[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color] % my_json_string)

[COLOR=#0000ff]# load and parse JSON[/color]
my_json = json.loads(my_json_string)
[COLOR=#008b8b]print[/color]([COLOR=#ff00ff]"[/color][COLOR=#ff00ff]VarProduct: %s[/color][COLOR=#ff00ff]"[/color] % my_json[[COLOR=#ff00ff]"[/color][COLOR=#ff00ff]Classes[/color][COLOR=#ff00ff]"[/color]][[COLOR=#ff00ff]0[/color]][[COLOR=#ff00ff]"[/color][COLOR=#ff00ff]Name[/color][COLOR=#ff00ff]"[/color]])
[COLOR=#008b8b]print[/color]([COLOR=#ff00ff]"[/color][COLOR=#ff00ff]VarScore: %s[/color][COLOR=#ff00ff]"[/color] % my_json[[COLOR=#ff00ff]"[/color][COLOR=#ff00ff]Classes[/color][COLOR=#ff00ff]"[/color]][[COLOR=#ff00ff]0[/color]][[COLOR=#ff00ff]"[/color][COLOR=#ff00ff]Score[/color][COLOR=#ff00ff]"[/color]])

Result:
Code:
$ python3 json_parse.py 
VarProduct: Loans
VarScore: 0.7328000068664551
 
@Mikrom your a Superstar! This works a treat. Thank you for your guidance!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top