Louis Sankey
Louis Sankey

Reputation: 492

google docs api - documentation shows how to 'dump' doc as JSON for troubleshooting, but JSON returned is different than JSON used to make requests

I'm writing a google doc using the google docs api, and I found that there is a json 'dump' feature that helps you understand the structure of your google doc.

The only problem is, the JSON dump is different than the JSON used to actually write the doc.

For example, this is where I found out about the JSON dump:

https://developers.google.com/docs/api/samples/output-json

The truncated JSON example looks like this:

{
    "body": {
        "content": [
            {
                "endIndex": 1, 
                "sectionBreak": {
                    "sectionStyle": {
                        "columnSeparatorStyle": "NONE", 
                        "contentDirection": "LEFT_TO_RIGHT"
                    }
                }
            }, 
            {
                "endIndex": 75, 
                "paragraph": {
                    "elements": [
                        {
                            "endIndex": 75, 
                            "startIndex": 1, 
                            "textRun": {
                                "content": "This is an ordinary paragraph. It is the first paragraph of the document.\n", 
                                "textStyle": {}
                            }
                        }
                    ], 
                    "paragraphStyle": {
                        "direction": "LEFT_TO_RIGHT", 
                        "namedStyleType": "NORMAL_TEXT"
                    }
                }, 
                "startIndex": 1
            }, 

And here is documentation for making update requests to your document:

https://developers.google.com/docs/api/reference/rest/v1/documents/request

Basically what I'm seeing is that the two have some general structures in common, but the notation for actually writing the JSON is different.

For instance, here's an example of how to write to the document:

      const updateObject = {
        documentId: 'documentID',
        resource: {
          requests: [ {
            insertText: {
              text: 'hello world',
              location: {
                index: 1,
              },
            },
          } ],
        },
      };

I'm hoping there is a way I can write the json in the same way that the dump is structured, and then pass that format through to create new documents. Otherwise I'm going to have to tediously translate everything from one format to the other (and there's a lot),

Does anyone have any advice on this? Maybe there is something I'm missing. Thanks!

Upvotes: 1

Views: 620

Answers (1)

Daniel
Daniel

Reputation: 3725

It appears to be intended. The JSON dump in the example matches the Document object. You can also find this same structure returned when you create a document via thedocuments.create API. Unfortunately, pretty much all of the fields are output-only. I tried to plug in the body from the sample into the API, and while it didn't fail, it also ignored the input and just created a basic document.

Meanwhile, the batchUpdate methods that you found to send update requests all seem focused on specific changes and none of them take a Document object. I think your best bet would be to follow up the feature request that you found, the Docs API is still considered in v1 after all.

Upvotes: 1

Related Questions