×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Extracting Values from a nested JSON

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

RE: Extracting Values from a nested JSON

Hi mmiah1982,

This works for me:
json_parse.py

CODE

import json
DBG_INFO = False

my_dictionary = {
"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}}"
}

my_body_string = my_dictionary["body"]
if DBG_INFO: print("my_body_string:\n%s\n" % my_body_string)

# for valid JSON replace single quotes with double quotes
my_json_string = my_body_string.replace("'", '"') 
if DBG_INFO: print("my_json_string:\n%s\n" % my_json_string)

# load and parse JSON
my_json = json.loads(my_json_string)
print("VarProduct: %s" % my_json["Classes"][0]["Name"])
print("VarScore: %s" % my_json["Classes"][0]["Score"]) 

Result:

CODE

$ python3 json_parse.py 
VarProduct: Loans
VarScore: 0.7328000068664551 

RE: Extracting Values from a nested JSON

(OP)
@Mikrom your a Superstar! This works a treat. Thank you for your guidance!

RE: Extracting Values from a nested JSON

@mmiah1982
You're welcome!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close