How can I save the output of Document Intelligence as a JSON file locally?

Harsh Khewal 105 Reputation points
2024-04-24T06:09:55.24+00:00

I have used Azure's Document Intelligence with my model to analyze a few documents. Now, I want to store this output in a JSON format locally. From what I have read online, the output given by Document Intelligence is already in JSON format. An example code that stores Doc Intelligence's output locally as a JSON file would be extremely helpful.

Azure AI Document Intelligence
Azure AI Document Intelligence
An Azure service that turns documents into usable data. Previously known as Azure Form Recognizer.
1,389 questions
{count} vote

Accepted answer
  1. santoshkc 4,425 Reputation points Microsoft Vendor
    2024-04-24T07:55:56.6433333+00:00

    Hi @Harsh Khewal,

    Thank you for reaching out to us with your query about saving the output of Azure's Document Intelligence as a JSON file locally. I'd be happy to help you with that.

    Here is an example code that uses the Azure Form Recognizer SDK to analyze a document and saved the output as a JSON file:

    from azure.core.credentials import AzureKeyCredential
    from azure.ai.formrecognizer import DocumentAnalysisClient
    
    import json
    
    # Your Azure Form Recognizer endpoint and API key
    endpoint = "<YOUR_ENDPOINT>"
    key = "<YOUR_API_KEY>"
    
    # Initialize the DocumentAnalysisClient
    credential = AzureKeyCredential(key)
    document_analysis_client = DocumentAnalysisClient(endpoint, credential)
    
    # The local file path to the document you want to analyze
    document_path = r"<path/to/your/document.pdf>"
    # Submitting the document for analysis
    with open(document_path, "rb") as f:
        analyze_result = document_analysis_client.begin_analyze_document("prebuilt-layout", document=f).result()
    
    # Convert the analysis result to JSON
    result_json = analyze_result.to_dict()
    
    # Save the JSON to a local file
    output_json_path = "output.json"
    with open(output_json_path, "w") as output_file:
        json.dump(result_json, output_file, indent=4)
    print("Analysis result saved to:", output_json_path)
    
    

    I hope this helps. Thank you.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful