Home > Blockchain >  How to read Json response into Document object of Document AI in C#
How to read Json response into Document object of Document AI in C#

Time:05-03

I am trying to read json file from cloud storage and trying to convert that into Google.Cloud.DocumentAI.V1.Document.

I have done POC, but its throwing exception Google.Protobuf.InvalidProtocolBufferException: 'Protocol message end-group tag did not match expected tag.'

First I am reading .Json file into MemoryStream and trying to Merge in to Document.

  using Google.Cloud.DocumentAI.V1;

  public static void StreamToDocument()
    {
        byte[] fileContents = File.ReadAllBytes("C:\\upload\\temp.json");
        
        using (Stream memoryStream = new MemoryStream(fileContents))
        {
            Document doc = new Document();
            var byteArray = memoryStream;
            doc.MergeFrom(byteArray);
        }           
    }

Error Message I am getting is

enter image description here

Is there any other way I can achieve this ?

CodePudding user response:

The code that you've specified there expects the data to be binary protobuf content. For JSON, you want:

string json = File.ReadAllText("C:\\upload\\temp.json");
Document document = Document.Parser.ParseJson(json);
  • Related