parse json output to simple text or variable - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: parse json output to simple text or variable (/thread-19339.html) |
parse json output to simple text or variable - murali_datascience - Jun-24-2019 Hi, I have below python code for face_recognition.basically it is comparing two images and providing match or not.I am getting output in json format as below.How to parse this json output to normal text or storing into a variable and display the out like thanks in advance for your response present output: { "Not matched": [], "Matched": [ [ "anil1_kZJfP0k.JPG", { "Matching Percentage": "68.59" } ] ] } Expected output: instead of all the brackets simple text output like: response ={matched} matching percentage ={68%} import face_recognition import os def match(lis, img): images = lis image_to_be_matched = face_recognition.load_image_file(img) # encoded the loaded image into a feature vector image_to_be_matched_encoded = face_recognition.face_encodings( image_to_be_matched)[0] # iterate over each image resp = {"Matched": [], "Not matched": []} for image in images: print(images) #image = os.path.join(path, image) current_image = face_recognition.load_image_file(image) # encode the loaded image into a feature vector current_image_encoded = face_recognition.face_encodings(current_image)[0] # match your image with the image and check if it matches result = face_recognition.compare_faces( [image_to_be_matched_encoded], current_image_encoded) # check if it was a match face_distances = face_recognition.face_distance([image_to_be_matched_encoded], current_image_encoded) if result[0] == True: resp["Matched"].append([os.path.basename(image), {'Matching Percentage': str((1-face_distances[0])*100)[:5]}]) else: resp["Not matched"].append([os.path.basename(image), {'Matching Percentage': str((1-face_distances[0])*100)[:5]}]) for image in images: os.remove(image) os.remove(img) print(resp) return resp RE: parse json output to simple text or variable - perfringo - Jun-25-2019 This is just mental exercise about how to extract data using indices and keys: data = { ... "Not matched": [], ... "Matched": [ ... [ ... "anil1_kZJfP0k.JPG", ... { ... "Matching Percentage": "68.59" ... } ... ] ... ] ... } >>> f'response = {data["Matched"][0][0]}' 'response = anil1_kZJfP0k.JPG' >>> f'matching percentage = {data["Matched"][0][1]["Matching Percentage"]}' 'matching percentage = 68.59' |