Sachin Tiwari
Sachin Tiwari

Reputation: 11

Cloud Document AI key value pair response

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

Answers (2)

Jianeng Xu
Jianeng Xu

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

MattCristal
MattCristal

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

Related Questions