1

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 ?

tt0206
  • 747
  • 3
  • 9
  • 24
  • Does your JSON file follow this structure? https://cloud.google.com/document-ai/docs/reference/rest/v1/Document – Ricco D May 03 '22 at 00:29
  • yes, json structure as per Document type. This Json is DocumentAI output json. – tt0206 May 03 '22 at 02:24
  • Can you provide a sample JSON that could reproduce the issue? Just so the community can easily test this out. – Ricco D May 03 '22 at 05:25

1 Answers1

4

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);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • we have done it something like this. var txtOut = File.ReadAllText(rawJson); JsonParser.Settings settings = new JsonParser.Settings(10); Google.Protobuf.JsonParser parser = new JsonParser(settings); var d1 = parser.Parse(txtOut); – tt0206 May 03 '22 at 13:30
  • @tt0206: Yes, that'll be fine too (although I wouldn't expect the default recursion limit of 100 to actually cause a problem). – Jon Skeet May 03 '22 at 13:33