2

I am using Google Cloud Document AI's Invoice Parser. API response is google.cloud.documentai_v1.types.Document object. I tried to write below approaches for converting it to JSON but nothing works:

Jofre
  • 3,718
  • 1
  • 23
  • 31
kushagra
  • 131
  • 3
  • 10
  • 1
    try a `print(google.cloud.documentai_v1.types.GcsDocuments())` and see where that gets you. [Link to docs](https://googleapis.dev/python/documentai/latest/documentai_v1/types.html#google.cloud.documentai_v1.types.GcsDocuments) – Edo Akse Jun 23 '21 at 09:49
  • Try this https://stackoverflow.com/questions/66379426/google-api-python-convert-object-to-json `google.cloud.documentai_v1.types.Document` also has `__dict__` so it might be applicable. – Ricco D Jun 24 '21 at 05:57

1 Answers1

2

You should be able to get the JSON with the to_json method you mentioned, but you have to pass the document you want to get as a JSON file, such as

google.cloud.documentai_v1.Document.to_json(my_document)

which will output the JSON file.

bernatj
  • 97
  • 5
  • This will output the `Document` object as a JSON string. You can also add optional parameters to include all possible fields (even those that are empty) and to change the field names to fit typical JSON camelCase instead of Python snake_case Example: `document_json = google.cloud.documentai_v1.Document.to_json(document, including_default_value_fields=False, preserving_proto_field_name=False)` – Holt Skinner Jun 14 '22 at 18:34