Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Forms Recognizer - Saving output results SDK Python

When I used the API from Forms Recognizer, it returned a JSON file. Now, I am using Form Recognizer with SDK and Python, and it returns a data type that seems to be specific from the library azure.ai.formrecognizer.

Does anyone know how to save the data acquired from Form Recognizer SDK Python in a JSON file like the one received from Form Recognzier API?

from azure.ai.formrecognizer import FormRecognizerClient
from azure.identity import ClientSecretCredential

client_secret_credential = ClientSecretCredential(tenant_id, client_id, client_secret)
form_recognizer_client = FormRecognizerClient(endpoint, client_secret_credential)
with open(os.path.join(path, file_name), "rb") as fd:
    form = fd.read()
poller = form_recognizer_client.begin_recognize_content(form)
form_pages = poller.result()
like image 858
isacontreras Avatar asked Jul 30 '26 17:07

isacontreras


1 Answers

Thanks for your question! The Azure Form Recognizer SDK for Python provides helper methods like to_dict and from_dict on the models to facilitate converting the data type in the library to and from a dictionary. You can use the dictionary you get from the to_dict method directly or convert it to JSON.

For your example above, in order to get a JSON output you could do something like:

poller = form_recognizer_client.begin_recognize_content(form)
form_pages = poller.result()

d = [page.to_dict() for page in form_pages]
json_string = json.dumps(d)

I hope that answers your question, please let me know if you need more information related to the library.

Also, there's more information about our models and their methods on our documentation page here. You can use the dropdown to select the version of the library that you're using.

like image 99
cperalta Avatar answered Aug 03 '26 07:08

cperalta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!