Skip to Content

Read and parse Google Docs content in Jitterbit Studio

Introduction

The Google Docs connector retrieves documents from Google Docs as structured JSON data. The content field returned by a Get Docs activity contains the full document body and can be passed directly to an LLM as a string (see Process documents with AI). When you need more granular access, the document's nested structure can be traversed using GetJSONString to extract specific elements: individual paragraphs, @-mentioned contact names and email addresses, or other inline objects.

This guide covers three patterns:

  • Traversing the document's nested element structure with GetJSONString and indexed paths to assemble paragraph text.
  • Reading person elements to extract the name and email address of @-mentioned contacts.
  • Extracting a Google Doc ID from a Gmail message body when the document link arrives as part of an email notification.

This guide builds on Process documents with AI, which covers the basic Google Docs connection setup and the straightforward Source.content mapping. For Google Docs connection prerequisites, see Google Docs prerequisites.

Design pattern

Parsing document content uses two linked steps within a single operation:

flowchart LR A["Transformation
Map docid request"] --> B["Get Docs activity"] --> C["Transformation
Capture content as variable"] --> D["Script step
Traverse structure
Extract paragraphs and @-mentions"] D -->|"$fullText, $contacts"| E["Downstream operation
(LLM, Slack, etc.)"]

A request transformation supplies the document ID to the Get Docs activity. A second transformation captures the content response field as a global variable. The script step traverses the nested JSON structure to extract paragraph text and @-mentioned contact details, producing variables that downstream operations can use directly. Part 5 covers passing those variables to a downstream operation.

When the document ID is not known in advance (for example, when it arrives inside a Gmail notification), a preceding script step decodes the email body and extracts the ID before the transformation runs. Part 4 covers this case.

Part 1: Configure the Google Docs connection

Set up a Google Docs connection as described in Google Docs prerequisites and Google Docs connection.

After creating the connection, create a project variable to hold the document ID:

  1. In Studio, open the project actions menu and select Project Variables.
  2. Create a project variable named google_docs_document_id. Set its default value to the document ID of the document you want to fetch (visible in the Google Docs URL: https://docs.google.com/document/d/<document-id>/edit), or leave the value empty if the ID will be set at runtime as described in Part 4.

Reference this variable in scripts and transformations using the $ prefix as $google_docs_document_id.

Part 2: Fetch the document and capture the content

Create the Fetch Document operation

  1. In Studio, create a new operation. Name it Fetch Document or a similar name.

  2. On the design component palette, expand the Google Docs endpoint. Drag the Get Docs activity type onto the design canvas to create an activity instance.

  3. Double-click the activity to open its configuration.

  4. Name: Enter a name for the activity, for example Get Document Content.

  5. Click Next to review the data schemas, then click Finished.

Map the document ID

Add a request transformation before the Get Docs activity to supply the document ID. Map the docid request field to the google_docs_document_id project variable:

<trans>
$google_docs_document_id
</trans>

Capture the document content

After the Get Docs activity, add a transformation to capture the content response field as a global variable. Add this script node unmapped (not assigned to any target field) so that it runs as a side effect during the transformation:

<trans>
If(Source.errormsg != "",
    RaiseError("Google Docs error: " + Source.errormsg)
);
$docContent = Source.content;
</trans>

The content field contains the JSON representation of document.body.content: an array of structural elements such as paragraphs, section breaks, and tables. This value is now accessible as docContent to any subsequent script step in the same operation or chained operation.

Add the parsing script step

After the transformation on the design canvas, add a script step to the operation. You will add the traversal logic to this step in Part 3.

Part 3: Traverse the document structure

The docContent variable holds a JSON array of structural elements. Each element represents one block-level unit in the document. The path to reach text content within a paragraph is:

/[i]/paragraph/elements/[j]/textRun/content

Where i is the index into the top-level content array and j is the index into a paragraph's elements array. Not every content item contains a paragraph node (section breaks and tables do not), and not every paragraph element contains a textRun node (@-mentioned people use a person node instead). GetJSONString returns an empty string when a path does not exist, so both While loops terminate naturally when an index is out of bounds.

Extract paragraph text

Add the following script to the script step created in Part 2:

<trans>
i = 0;
$fullText = "";
$element = GetJSONString($docContent, "/[" + i + "]");
While($element != "" && $element != "null",
    j = 0;
    $textPart = GetJSONString($docContent, "/[" + i + "]/paragraph/elements/[" + j + "]/textRun/content");
    While($textPart != "" && $textPart != "null",
        $fullText += TrimChars($textPart, "\"");
        j++;
        $textPart = GetJSONString($docContent, "/[" + i + "]/paragraph/elements/[" + j + "]/textRun/content");
    );
    i++;
    $element = GetJSONString($docContent, "/[" + i + "]");
);
</trans>

TrimChars removes the surrounding double-quote characters that GetJSONString includes in its output for string values. After the script completes, fullText contains the document's full text with paragraph line breaks preserved. Pass fullText as the prompt body to a downstream LLM operation. For LLM setup, see Use OpenAI to process data in a Studio operation or Use Azure OpenAI in a Studio operation.

Extract @-mentioned contacts

When a Google Doc contains @-mentioned people, each mention appears as a person element inside a paragraph's elements array. The personProperties child node holds the display name and email address of the contact.

Append the following to the script step, after the paragraph text loop:

<trans>
i = 0;
$contacts = "";
$element = GetJSONString($docContent, "/[" + i + "]");
While($element != "" && $element != "null",
    j = 0;
    $personNode = GetJSONString($docContent, "/[" + i + "]/paragraph/elements/[" + j + "]/person");
    While($personNode != "" && $personNode != "null",
        $personName = TrimChars(
            GetJSONString($docContent, "/[" + i + "]/paragraph/elements/[" + j + "]/person/personProperties/name"),
            "\"");
        $personEmail = TrimChars(
            GetJSONString($docContent, "/[" + i + "]/paragraph/elements/[" + j + "]/person/personProperties/email"),
            "\"");
        If($personName != "" && $personName != "null",
            $contacts += $personName + " <" + $personEmail + ">\n";
        );
        j++;
        $personNode = GetJSONString($docContent, "/[" + i + "]/paragraph/elements/[" + j + "]/person");
    );
    i++;
    $element = GetJSONString($docContent, "/[" + i + "]");
);
</trans>

After the script completes, contacts contains a newline-separated list of display names and email addresses for all @-mentioned people in the document.

Part 4: Extract a Google Doc ID from a Gmail email body

When a Studio operation receives an email notification that contains a link to a Google Doc, the document ID can be extracted from the email body before the Fetch Document operation runs.

Gmail API responses encode email bodies using base64url encoding, which substitutes - for + and _ for / compared to standard base64. Decoding requires reversing these substitutions before calling Base64Decode.

Add the following script in a script step that runs before the Fetch Document operation:

<trans>
// $emailBodyBase64 holds the base64url-encoded email body from the Gmail API response

// Normalize base64url encoding to standard base64
$emailBodyBase64Clean = Replace($emailBodyBase64, "-", "+");
$emailBodyBase64Clean = Replace($emailBodyBase64Clean, "_", "/");

// Decode binary content to a plain text string
$emailBodyText = HexToString(BinaryToHex(Base64Decode($emailBodyBase64Clean)));

// Locate the Google Docs URL pattern and extract the 44-character document ID
$urlPrefix = "docs.google.com/document/d/";
$startPos = Index($emailBodyText, $urlPrefix) + Length($urlPrefix);
$google_docs_document_id = Mid($emailBodyText, $startPos, 44);
</trans>

Replace corrects the base64url character substitution. The Base64DecodeBinaryToHexHexToString chain converts binary-decoded content to a readable string. Index finds the character position of the URL prefix, Length advances past it, and Mid extracts the document ID at that position.

The decoded email body is typically HTML. The docs.google.com/document/d/ pattern appears in anchor tag href attributes, and Google Docs document IDs are consistently 44 characters long. After this script completes, google_docs_document_id is set and the Fetch Document operation can run.

Note

This extraction pattern applies to HTML email bodies returned by the Gmail API. If the email body arrives in a different format (for example, plain text or a URL-shortened link), adapt the Index search string and the Mid character count accordingly.

Part 5: Use the extracted content downstream

After the parsing script step runs, fullText and contacts hold the extracted document text and @-mentioned contacts. Chain a downstream operation on the success of the Fetch Document operation to use these values:

Because fullText and contacts are global variables, they are available to any operation chained after the Fetch Document operation without additional mapping.

Verify the integration

  1. Deploy and run the Fetch Document operation.

  2. In the operation logs, confirm the Get Document Content activity completed successfully.

  3. Confirm that fullText contains the expected document text. Add WriteToOperationLog($fullText) temporarily to the script step to inspect the value in the operation log.

  4. If the document contains @-mentions, confirm contacts is non-empty and contains the expected names and email addresses.

  5. If the Get Docs activity returns an empty or null content field:

    • Confirm google_docs_document_id is set to the correct 44-character document ID.
    • Confirm the service account configured in the Google Docs connection has read access to the document. Share the document with the service account's client_email address directly in Google Docs if necessary.
  6. If fullText contains unexpected gaps, note that structural elements such as section breaks and table cells produce content items with no paragraph node. These are skipped by the outer While loop because GetJSONString returns an empty string for the missing path.