Inspecting structured text for sensitive data Stay organized with collections Save and categorize content based on your preferences.
Sensitive Data Protection can detect and classify sensitive data within structuredcontent such as CSV. By inspecting or de-identifying as a table, thestructure and columns provide Sensitive Data Protection with additional clues thatmay enable it to provide better results for some use cases.
Inspecting a table
The code samples below demonstrate how to check a table of data for sensitivecontent.Tablessupport a variety oftypes.
Important: The code on this page requires that you first set up a Sensitive Data Protection client. For more information about installing and creating a Sensitive Data Protection client, seeSensitive Data Protection client libraries. (Sending JSON to Sensitive Data Protection REST endpoints does not require a client library.)
C#
To learn how to install and use the client library for Sensitive Data Protection, seeSensitive Data Protection client libraries.
To authenticate to Sensitive Data Protection, set up Application Default Credentials. For more information, seeSet up authentication for a local development environment.
usingSystem;usingSystem.Collections.Generic;usingGoogle.Api.Gax.ResourceNames;usingGoogle.Cloud.Dlp.V2;publicclassInspectTable{publicstaticInspectContentResponseInspectTableData(stringprojectId,TabletableToInspect=null,IEnumerable<InfoType>infoTypes=null){// Instantiate a client.vardlp=DlpServiceClient.Create();// Construct the table if null.if(tableToInspect==null){varrow1=newValue[]{newValue{StringValue="John Doe"},newValue{StringValue="(206) 555-0123"}};varrow2=newValue[]{newValue{StringValue="Mark Twain"},newValue{StringValue="(450) 555-0123"}};tableToInspect=newTable{Headers={newFieldId{Name="Name"},newFieldId{Name="Phone"}},Rows={newTable.Types.Row{Values={row1}},newTable.Types.Row{Values={row2}}}};}// Set content item.varcontentItem=newContentItem{Table=tableToInspect};// Construct inspect config.varinspectConfig=newInspectConfig{InfoTypes={infoTypes??newInfoType[]{newInfoType{Name="PHONE_NUMBER"}}},IncludeQuote=true,};// Construct a request.varrequest=newInspectContentRequest{ParentAsLocationName=newLocationName(projectId,"global"),InspectConfig=inspectConfig,Item=contentItem,};// Call the API.varresponse=dlp.InspectContent(request);// Inspect the results.varresultFindings=response.Result.Findings;Console.WriteLine($"Findings: {resultFindings.Count}");foreach(varfinresultFindings){Console.WriteLine("Quote: "+f.Quote);Console.WriteLine("Info type: "+f.InfoType.Name);Console.WriteLine("Likelihood: "+f.Likelihood);}returnresponse;}}Go
To learn how to install and use the client library for Sensitive Data Protection, seeSensitive Data Protection client libraries.
To authenticate to Sensitive Data Protection, set up Application Default Credentials. For more information, seeSet up authentication for a local development environment.
import("context""fmt""io"dlp"cloud.google.com/go/dlp/apiv2""cloud.google.com/go/dlp/apiv2/dlppb")// inspectTable inspects a table for sensitive contentfuncinspectTable(wio.Writer,projectIDstring)error{// projectID := "your-project-id"ctx:=context.Background()// Initialize a client once and reuse it to send multiple requests. Clients// are safe to use across goroutines. When the client is no longer needed,// call the Close method to cleanup its resources.client,err:=dlp.NewClient(ctx)iferr!=nil{returnerr}// Closing the client safely cleans up background resources.deferclient.Close()// create a default tabletableToInspect:=&dlppb.Table{Headers:[]*dlppb.FieldId{{Name:"name"},{Name:"phone"},},Rows:[]*dlppb.Table_Row{{Values:[]*dlppb.Value{{Type:&dlppb.Value_StringValue{StringValue:"John Doe",},},{Type:&dlppb.Value_StringValue{StringValue:"(206) 555-0123",},},},},},}// Specify the table to be inspected.contentItem:=&dlppb.ContentItem{DataItem:&dlppb.ContentItem_Table{Table:tableToInspect,},}// Specify the type of info the inspection will look for.// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info typesinfoTypes:=[]*dlppb.InfoType{{Name:"PHONE_NUMBER"},}// Construct the Inspect request to be sent by the client.req:=&dlppb.InspectContentRequest{Parent:fmt.Sprintf("projects/%s/locations/global",projectID),Item:contentItem,InspectConfig:&dlppb.InspectConfig{InfoTypes:infoTypes,IncludeQuote:true,},}// Send the request.resp,err:=client.InspectContent(ctx,req)iferr!=nil{returnerr}// Print the results.fmt.Fprintf(w,"Findings: %v\n",len(resp.Result.Findings))for_,v:=rangeresp.GetResult().Findings{fmt.Fprintf(w,"Quote: %v\n",v.GetQuote())fmt.Fprintf(w,"Infotype Name: %v\n",v.GetInfoType().GetName())fmt.Fprintf(w,"Likelihood: %v\n",v.GetLikelihood())}returnnil}Java
To learn how to install and use the client library for Sensitive Data Protection, seeSensitive Data Protection client libraries.
To authenticate to Sensitive Data Protection, set up Application Default Credentials. For more information, seeSet up authentication for a local development environment.
importcom.google.cloud.dlp.v2.DlpServiceClient;importcom.google.privacy.dlp.v2.ContentItem;importcom.google.privacy.dlp.v2.FieldId;importcom.google.privacy.dlp.v2.Finding;importcom.google.privacy.dlp.v2.InfoType;importcom.google.privacy.dlp.v2.InspectConfig;importcom.google.privacy.dlp.v2.InspectContentRequest;importcom.google.privacy.dlp.v2.InspectContentResponse;importcom.google.privacy.dlp.v2.LocationName;importcom.google.privacy.dlp.v2.Table;importcom.google.privacy.dlp.v2.Table.Row;importcom.google.privacy.dlp.v2.Value;publicclassInspectTable{publicstaticvoidmain(String[]args)throwsException{// TODO(developer): Replace these variables before running the sample.StringprojectId="your-project-id";TabletableToInspect=Table.newBuilder().addHeaders(FieldId.newBuilder().setName("name").build()).addHeaders(FieldId.newBuilder().setName("phone").build()).addRows(Row.newBuilder().addValues(Value.newBuilder().setStringValue("John Doe").build()).addValues(Value.newBuilder().setStringValue("(206) 555-0123").build())).build();inspectTable(projectId,tableToInspect);}// Inspects the provided text.publicstaticvoidinspectTable(StringprojectId,TabletableToInspect){// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests. After completing all of your requests, call// the "close" method on the client to safely clean up any remaining background resources.try(DlpServiceClientdlp=DlpServiceClient.create()){// Specify the table to be inspected.ContentItemitem=ContentItem.newBuilder().setTable(tableToInspect).build();// Specify the type of info the inspection will look for.// See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info typesInfoTypeinfoType=InfoType.newBuilder().setName("PHONE_NUMBER").build();// Construct the configuration for the Inspect request.InspectConfigconfig=InspectConfig.newBuilder().addInfoTypes(infoType).setIncludeQuote(true).build();// Construct the Inspect request to be sent by the client.InspectContentRequestrequest=InspectContentRequest.newBuilder().setParent(LocationName.of(projectId,"global").toString()).setItem(item).setInspectConfig(config).build();// Use the client to send the API request.InspectContentResponseresponse=dlp.inspectContent(request);// Parse the response and process resultsSystem.out.println("Findings: "+response.getResult().getFindingsCount());for(Findingf:response.getResult().getFindingsList()){System.out.println("\tQuote: "+f.getQuote());System.out.println("\tInfo type: "+f.getInfoType().getName());System.out.println("\tLikelihood: "+f.getLikelihood());}}catch(Exceptione){System.out.println("Error during inspectString: \n"+e.toString());}}}Node.js
To learn how to install and use the client library for Sensitive Data Protection, seeSensitive Data Protection client libraries.
To authenticate to Sensitive Data Protection, set up Application Default Credentials. For more information, seeSet up authentication for a local development environment.
// Imports the Google Cloud Data Loss Prevention libraryconstDLP=require('@google-cloud/dlp');// Instantiates a clientconstdlp=newDLP.DlpServiceClient();// The project ID to run the API call under// const projectId = 'my-project';// The infoTypes of information to matchconstinfoTypes=[{name:'PHONE_NUMBER'}];// Table dataconsttableData={headers:[{name:'name'},{name:'phone'}],rows:[{values:[{stringValue:'John Doe'},{stringValue:'(206) 555-0123'}],},],};asyncfunctioninspectTable(){// Specify the table to be inspected.constitem={table:tableData,};// Construct the configuration for the Inspect request.constinspectConfig={infoTypes:infoTypes,includeQuote:true,};// Construct the Inspect request to be sent by the client.constrequest={parent:`projects/${projectId}/locations/global`,inspectConfig:inspectConfig,item:item,};// Use the client to send the API request.const[response]=awaitdlp.inspectContent(request);// Print findings.constfindings=response.result.findings;if(findings.length >0){console.log(`Findings:${findings.length}\n`);findings.forEach(finding=>{console.log(`InfoType:${finding.infoType.name}`);console.log(`\tQuote:${finding.quote}`);console.log(`\tLikelihood:${finding.likelihood} \n`);});}else{console.log('No findings.');}}inspectTable();PHP
To learn how to install and use the client library for Sensitive Data Protection, seeSensitive Data Protection client libraries.
To authenticate to Sensitive Data Protection, set up Application Default Credentials. For more information, seeSet up authentication for a local development environment.
use Google\Cloud\Dlp\V2\Client\DlpServiceClient;use Google\Cloud\Dlp\V2\ContentItem;use Google\Cloud\Dlp\V2\FieldId;use Google\Cloud\Dlp\V2\InfoType;use Google\Cloud\Dlp\V2\InspectConfig;use Google\Cloud\Dlp\V2\InspectContentRequest;use Google\Cloud\Dlp\V2\Likelihood;use Google\Cloud\Dlp\V2\Table;use Google\Cloud\Dlp\V2\Table\Row;use Google\Cloud\Dlp\V2\Value;/** * Inspect a table for sensitive content. * * @param string $projectId The Google Cloud project id to use as a parent resource. */function inspect_table(string $projectId): void{ // Instantiate a client. $dlp = new DlpServiceClient(); $parent = "projects/$projectId/locations/global"; // Specify the table to be inspected. $tableToDeIdentify = (new Table()) ->setHeaders([ (new FieldId()) ->setName('NAME'), (new FieldId()) ->setName('PHONE'), ]) ->setRows([ (new Row())->setValues([ (new Value()) ->setStringValue('John Doe'), (new Value()) ->setStringValue('(206) 555-0123') ]) ]); $item = (new ContentItem()) ->setTable($tableToDeIdentify); // Construct the configuration for the Inspect request. $phoneNumber = (new InfoType()) ->setName('PHONE_NUMBER'); $inspectConfig = (new InspectConfig()) ->setInfoTypes([$phoneNumber]) ->setIncludeQuote(true); // Run request. $inspectContentRequest = (new InspectContentRequest()) ->setParent($parent) ->setInspectConfig($inspectConfig) ->setItem($item); $response = $dlp->inspectContent($inspectContentRequest); // Print the results. $findings = $response->getResult()->getFindings(); if (count($findings) == 0) { printf('No findings.' . PHP_EOL); } else { printf('Findings:' . PHP_EOL); foreach ($findings as $finding) { printf(' Quote: %s' . PHP_EOL, $finding->getQuote()); printf(' Info type: %s' . PHP_EOL, $finding->getInfoType()->getName()); printf(' Likelihood: %s' . PHP_EOL, Likelihood::name($finding->getLikelihood())); } }}Python
To learn how to install and use the client library for Sensitive Data Protection, seeSensitive Data Protection client libraries.
To authenticate to Sensitive Data Protection, set up Application Default Credentials. For more information, seeSet up authentication for a local development environment.
fromtypingimportList,Optionalimportgoogle.cloud.dlpdefinspect_table(project:str,data:str,info_types:List[str],custom_dictionaries:List[str]=None,custom_regexes:List[str]=None,min_likelihood:Optional[str]=None,max_findings:Optional[int]=None,include_quote:bool=True,)->None:"""Uses the Data Loss Prevention API to analyze strings for protected data. Args: project: The Google Cloud project id to use as a parent resource. data: Json string representing table data. info_types: A list of strings representing info types to look for. A full list of info type categories can be fetched from the API. min_likelihood: A string representing the minimum likelihood threshold that constitutes a match. One of: 'LIKELIHOOD_UNSPECIFIED', 'VERY_UNLIKELY', 'UNLIKELY', 'POSSIBLE', 'LIKELY', 'VERY_LIKELY'. max_findings: The maximum number of findings to report; 0 = no maximum. include_quote: Boolean for whether to display a quote of the detected information in the results. Returns: None; the response from the API is printed to the terminal. Example: data = { "header":[ "email", "phone number" ], "rows":[ [ "robertfrost@xyz.com", "4232342345" ], [ "johndoe@pqr.com", "4253458383" ] ] } >> $ python inspect_content.py table \ '{"header": ["email", "phone number"], "rows": [["robertfrost@xyz.com", "4232342345"], ["johndoe@pqr.com", "4253458383"]]}' >> Quote: robertfrost@xyz.com Info type: EMAIL_ADDRESS Likelihood: 4 Quote: johndoe@pqr.com Info type: EMAIL_ADDRESS Likelihood: 4 """# Instantiate a client.dlp=google.cloud.dlp_v2.DlpServiceClient()# Prepare info_types by converting the list of strings into a list of# dictionaries (protos are also accepted).info_types=[{"name":info_type}forinfo_typeininfo_types]# Prepare custom_info_types by parsing the dictionary word lists and# regex patterns.ifcustom_dictionariesisNone:custom_dictionaries=[]dictionaries=[{"info_type":{"name":f"CUSTOM_DICTIONARY_{i}"},"dictionary":{"word_list":{"words":custom_dict.split(",")}},}fori,custom_dictinenumerate(custom_dictionaries)]ifcustom_regexesisNone:custom_regexes=[]regexes=[{"info_type":{"name":f"CUSTOM_REGEX_{i}"},"regex":{"pattern":custom_regex},}fori,custom_regexinenumerate(custom_regexes)]custom_info_types=dictionaries+regexes# Construct the configuration dictionary. Keys which are None may# optionally be omitted entirely.inspect_config={"info_types":info_types,"custom_info_types":custom_info_types,"min_likelihood":min_likelihood,"include_quote":include_quote,"limits":{"max_findings_per_request":max_findings},}# Construct the `table`. For more details on the table schema, please see# https://cloud.google.com/dlp/docs/reference/rest/v2/ContentItem#Tableheaders=[{"name":val}forvalindata["header"]]rows=[]forrowindata["rows"]:rows.append({"values":[{"string_value":cell_val}forcell_valinrow]})table={}table["headers"]=headerstable["rows"]=rowsitem={"table":table}# Convert the project id into a full resource id.parent=f"projects/{project}"# Call the API.response=dlp.inspect_content(request={"parent":parent,"inspect_config":inspect_config,"item":item})# Print out the results.ifresponse.result.findings:forfindinginresponse.result.findings:try:iffinding.quote:print(f"Quote:{finding.quote}")exceptAttributeError:passprint(f"Info type:{finding.info_type.name}")print(f"Likelihood:{finding.likelihood}")else:print("No findings.")REST
See theJSON quickstart for more information aboutusing the DLP API with JSON.
JSON Input:
POSThttps://dlp.googleapis.com/v2/projects/[PROJECT_ID]/content:inspect?key={YOUR_API_KEY}{"item":{"table":{"headers":[{"name":"name"}, {"name":"phone"}],"rows":[{ "values":[ {"string_value": "John Doe"}, {"string_value": "(206) 555-0123"}]}],}},"inspectConfig":{"infoTypes":[ { "name":"PHONE_NUMBER" }],"includeQuote":true}}JSON Output:
{ "result": { "findings": [ { "quote": "(206) 555-0123", "infoType": { "name": "PHONE_NUMBER" }, "likelihood": "VERY_LIKELY", "location": { "byteRange": { "end": "14" }, "codepointRange": { "end": "14" }, "contentLocations": [ { "recordLocation": { "fieldId": { "name": "phone" }, "tableLocation": { } } } ] }, "createTime": "2019-03-08T23:55:10.980Z" } ] }}Text versus structured text
Structuring text can help provide context. The same request as the one in theprevious example, if inspected as a string—that is, as just "John Doe,(206) 555-0123"—would provide less accurate findings. That's becauseSensitive Data Protection has fewer contextual clues about what the purpose of thenumber might be. When possible, consider parsing your strings into a tableobject for the most accurate scan results.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-15 UTC.