0

I'm working on GCP Document AI using Node.js and react.js, In the given code I have created JSON structure (var jsonResult) then in for loop I get all the different key and text value data only if I do console.log(key); and console.log(textValue); but I want to store all those data as JSON, then I want to send that JSON as response using Node.js snippet res.json(JSON.stringify(jsonResult));.

var jsonResult = [{
           key:" ",
           value:" "
        }];

console.log('Document processing complete.');

const {document} = result;
for (let i=0;i<document.entities.length;i++)
{
  var key = document.entities[i].type;
  var textValue = document.entities[i].textAnchor !== null ? document.entities[i].textAnchor.content : '';

  // console.log(key);
  // console.log(textValue);

  jsonResult[i].key = key;
  jsonResult[i].textValue = textValue;
}

res.json(JSON.stringify(jsonResult));
double-beep
  • 5,031
  • 17
  • 33
  • 41
rakshixh
  • 11
  • 1

1 Answers1

0
const data = result.document.entities;
let jsonArray = [];
for (let i = 0; i < data.length; i++)
{
  let key = data[i]?.type;
  let value = !data[i]?.textAnchor ? data[i]?.textAnchor?.content : '';

  if (key && value)
    jsonArray.push({key: key, textValue: value });

}
res.json(JSON.stringify(jsonArray));
Ali Azmoodeh
  • 152
  • 2
  • 10
  • FYI, Document AI has an actively monitored tag [`[cloud-document-ai]`](https://stackoverflow.com/questions/tagged/cloud-document-ai) --- This answer should work, refer to the documentation https://cloud.google.com/document-ai/docs/handle-response#entities_nested_entities_and_normalized_values for more info on handling the response and getting text from entities. – Holt Skinner Mar 28 '23 at 21:48