Reputation: 11
Google cloud document AI is supposed to give response in key value pair form like this.DOC-AI console output
But when we hit its api then it gives us response in json(which is obviously key value) but i want the text response as shown in the upper pic like key value pair. The response by postman api is as follows: Postman Output Now here i want that text section as key value pair like against invoice id i want invoice id.
Is there is any solution regarding this?
Upvotes: 1
Views: 1180
Reputation: 117
It should be like this, you can get the k-v pair from the formFields and then build the map you want
ProcessRequest request = ProcessRequest.newBuilder().setName(name).setRawDocument(document).build();
ProcessResponse result = client.processDocument(request);
Document documentResponse = result.getDocument();
Map<String, String> resultMap = new HashMap<>();
for (Document.Page page : documentResponse.getPagesList()) {
List<Document.Page.FormField> formFields = page.getFormFieldsList();
for (Document.Page.FormField formField : formFields) {
String key = formField.getFieldName().getTextAnchor().getContent();
String value = formField.getFieldValue().getTextAnchor().getContent();
resultMap.put(key, value);
}
}
Upvotes: 0
Reputation: 21
I was also struggling to understand this. What I did is to upload the JSON response to something like https://jsonformatter.curiousconcept.com/# and inspect the document.
As you can see, in "pages"
you can find the various formFields
in which you can find the relevant key-value pairs.
Upvotes: 2