Amazon Textract Response Parser
repository·master·Indexed 42 days ago
https://github.com/aws-samples/amazon-textract-response-parserA library (version 0.4.3) that simplifies parsing JSON responses from Amazon Textract by providing high-level, language-specific abstractions for document elements such as forms, tables, lines, and words. It supports Python, JavaScript/TypeScript, and C# (.NET Core), allowing developers to easily access document data without manually traversing complex JSON structures.
What's inside Amazon Textract Response Parser
- The Textract-Amazon Augmented AI (A2I) response parser library allows you to easily parse the JSON returned by an A2I human review workflow. The library processes the JSON and provides language-specific constructs (available in Python, JavaScript/TypeScript, and C#) to interact with different parts of the document, such as forms and tables.
Overview of Textract Response Parser
masterThe Textract Response Parser is a library designed to simplify the process of parsing JSON responses returned by Amazon Textract. Instead of manually traversing complex JSON structures, the library provides language-specific constructs (Python, JavaScript/TypeScript, and C#) to easily access and work with different parts of a document, such as lines, words, forms, and tables.Navigate document text using TRP helper methods
masterThe library provides structured ways to navigate the document hierarchy instead of exposing raw arrays directly. This helps avoid accidental mutations and provides cleaner access patterns:
.n***properties: Count items (e.g.,doc.nPages)..list***(): Returns a copy of the underlying array (e.g.,doc.listPages())..iter***(): Returns an iterator for collections (e.g.,page.iterLines())..***At***(): Fetches a specific item by index (e.g.,table.cellAt(2, 4)).
// Navigate the document hierarchy: console.log(`Opened doc with ${doc.nPages} pages`); console.log( `The first word of the first line is ${doc.pageNumber(1).lineAtIndex(0).wordAtIndex(0).text}` ); // Iterate through content: for (const page of doc.iterPages()) { for (const line of page.iterLines()) { for (const word of line.iterWords()) { console.log(word.text); } } } // Get snapshot arrays instead of iterators: const linesArrsByPage = doc.listPages().map((p) => p.listLines());How mutation operations work in Textract Response Parser
masterThe library allows you to augment or edit Textract JSON objects using its classes (e.g.,
TextractDocument,Page,Word). When using mutation operations provided by the library, keep the following behaviors in mind:- In-place modification: Mutation operations provided by library classes modify the source API JSON object in-place and attempt to ensure self-consistency.
- Accessing raw JSON: You can access the underlying API JSON object for any class via the
.dictproperty (e.g.,word.dict,table.dict). If you modify the JSON directly via.dict, you are responsible for updating any required references in other objects to maintain consistency. - Dynamic reflection: Individual block-level changes made to the underlying API JSON (like overriding word text or coordinates) are dynamically reflected in the parsed TRP objects. However, changes affecting inter-block relationships may cause staleness issues.
- Array properties: To prevent unsupported arbitrary mutations (like
table.rows.pop()), the library generally exposes getters and iterators rather than direct access to lists. Use methods liketable.nRows,table.iterRows(),table.listRows(), ortable.cellsAt()instead of attempting to manipulate arrays directly.
How Serializer/Deserializer works with Textract JSON
masterThe library uses the
marshmallowframework to convert raw Textract JSON dictionaries into structured Python objects (and vice versa).- Use
TDocumentSchema().load(j)to deserialize a standard Textract JSON dictionaryjinto aTDocumentobject. - Use
TDocumentSchema().dump(t_doc)to serialize aTDocumentobject back into a JSON-compatible dictionary. - For AnalyzeID responses, use
TAnalyzeIdDocumentSchemainstead ofTDocumentSchema.
# Deserialize standard Textract JSON from trp.trp2 import TDocument, TDocumentSchema t_doc = TDocumentSchema().load(j) # Serialize back to JSON t_doc = TDocumentSchema().dump(t_doc) # For AnalyzeID from trp.trp2_analyzeid import TAnalyzeIdDocument, TAnalyzeIdDocumentSchema t_doc = TAnalyzeIdDocumentSchema().load(json.loads(j))- Use
Handle multi-page documents with Asynchronous Textract APIs
masterThe synchronous request/response APIs used in many basic examples are limited to images or single-page documents. For multi-page documents or large batches, you must use the Asynchronous Textract APIs.
Workflow for Asynchronous Processing:
- Upload: Upload the source document to an Amazon S3 bucket.
- Start Analysis: Call
StartDocumentAnalysis(or similar async commands) which returns a job ID. - Retrieve Results: Use the job ID with
GetDocumentAnalysisto retrieve the results once the job is complete.
Best Practices for Scale:
- Avoid Polling: Instead of repeatedly calling
GetDocumentAnalysis, use Amazon SNS notifications to trigger your application when the analysis is ready. This is more quota-efficient. - Manage Quotas: Be aware of Amazon Textract quota limits. For spiky workloads, implement a queuing mechanism to manage request rates.
- Infrastructure: A production-ready solution typically involves AWS Lambda and Amazon SNS topics.
Understand the parsed Form data structure
masterWhen parsing Amazon Textract responses for Forms, the output provides a structured mapping of keys to their corresponding values. Each field entry typically contains:
key: A dictionary representing the label (e.g., 'Phone Number:'), including itsblockmetadata (type, confidence, geometry) andcontent(the individual text segments making up the key).value: A dictionary representing the extracted value (e.g., '555-0100'), including itsblockmetadata andcontent.
This structure allows you to programmatically access specific fields by their text label or by navigating the block relationships.
Parse Amazon Textract A2I Human Review responses in Python
masterThe Textract-Augmented AI (A2I) response parser library allows you to parse the JSON returned by an A2I human review workflow. It provides language-specific constructs to navigate document elements like forms and fields.
To use the parser, you must first obtain the human review results from S3 (which are published after the human review is complete) and extract the specific Textract response segment from the A2I JSON structure. Once extracted, you can initialize a
Documentobject to iterate through pages and fields.# 1. Extract the Textract response from the A2I JSON # The path in the JSON is typically: a2i_json["humanAnswers"][0]["answerContent"]["AWS/Textract/AnalyzeDocument/Forms/V1"] a2i_response = a2i_json["humanAnswers"][0]["answerContent"]["AWS/Textract/AnalyzeDocument/Forms/V1"] # 2. Initialize the Document object doc = Document(a2i_response) # 3. Iterate over elements for page in doc.pages: for field in page.form.fields: k = field.key.text if field.key else "" v = field.value.text if field.value else "" print(f"Field: Key: {k}, Value: {v}")Import Textract Response Parser in Node.js or Browser
masterDepending on your environment, you can import the library using CommonJS
requireor ES6importstatements. For direct browser usage without a module framework, you can link to a CDN like unpkg.// With CommonJS-style require: const { TextractDocument, TextractIdentity } = require("amazon-textract-response-parser"); // Or ES-style module imports: import { TextractDocument, TextractExpense } from "amazon-textract-response-parser";<script src="https://unpkg.com/amazon-textract-response-parser@x.y.z"></script> <script> // Use the main parser classes: var doc = new trp.TextractDocument(...); // Or other exported utility functions/classes/enums/etc: var avg = trp.aggregate([1, 2, 3], trp.AggregationMethod.Mean); </script>Install amazon-textract-response-parser via pip
masterInstall the Python library using pip to begin parsing Amazon Textract JSON responses.
python -m pip install amazon-textract-response-parserRun tests for Textract Response Parser
masterYou can run tests using
npmto validate the library. Note that the full test suite includes integration tests that call Amazon Textract APIs.Full Integration Tests
To run
npm run test, your environment must be configured with AWS credentials (e.g., via the AWS CLI). Warning: This will make billable API requests to Amazon Textract.Unit Tests
To run only the local unit tests without making AWS API calls, use:
npm run test:unitSwitch from local TRP.js build to published version
masterTo use a published version of
amazon-textract-response-parserinstead of the local source code used in these examples, follow these steps based on your environment:NodeJS Projects
In your
package.json, replace the relative file path with a version specifier and reinstall dependencies:From:
"amazon-textract-response-parser": "file:../.."To:
"amazon-textract-response-parser": "^0.4.3"(or your desired version)Then run:
npm installBrowser IIFE Projects
Edit the
<script>tag in your HTML file to point to a CDN URL or a local path to a downloadedtrp.min.jsfile.{ "dependencies": { "amazon-textract-response-parser": "^0.4.3" } }