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 ?

3
  • Does your JSON file follow this structure? cloud.google.com/document-ai/docs/reference/rest/v1/Document Commented May 3, 2022 at 0:29
  • yes, json structure as per Document type. This Json is DocumentAI output json. Commented May 3, 2022 at 2:24
  • Can you provide a sample JSON that could reproduce the issue? Just so the community can easily test this out. Commented May 3, 2022 at 5:25

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

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<Document>(txtOut);
@tt0206: Yes, that'll be fine too (although I wouldn't expect the default recursion limit of 100 to actually cause a problem).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.