データソースタグの例
データソースの AMPscript の例
次の例では、データソースタグで読み取られるオブジェクトのコレクションを使用して、AMPscript 変数から情報を読み込みます。
1%%[ var @Json set @Json = ' [{"emailaddress":"john@example.com","Region":"West","State":"California","City":"San Francisco"},
2{"emailaddress":"carla@example.com","Region":"Central","State":"Indiana","City":"Indianapolis"}]' ]%%
3
4{{.datasource JSONVar type=variable maxRows = 20}}
5 {{.data}}
6 { "target" : "@Json" }
7 {{/data}}
8 Email Address: {{emailaddress}}
9 Region: {{region}}
10 State: {{STATE}}
11 City: {{JSONVar.City}}
12{{/datasource}}この例では、次の情報が生成されます。
1Email Address: carla@example.com
2Region: Central
3State: Indiana
4City: Indianapolis1Email Address: john@example.com
2Region: West
3State: California
4City: San Franciscoデータソースのサーバーサイド JavaScript の例
この例では、データソースタグを使用して、サーバーサイド JavaScript 変数から JSON データを読み込みます。
1<script runat=server> var Json = '{"emailaddress":"carla@example.com","Region":"Central","State":"Indiana","City":"Indianapolis"}';
2</script>
3
4{{.datasource JSONVar type=variable maxRows = 20}}
5 {{.data}}
6 { "target" : "Json" }
7 {{/data}}
8Email Address: {{emailaddress}}
9Region: {{region}}
10State: {{STATE}}
11City: {{JSONVar.City}}
12{{/datasource}}この例では、次の情報が生成されます。
1Email Address: carla@example.com
2Region: Central
3State: Indiana
4City: IndianapolisJSON データのフィルターおよび並び替えの例
次の例では、データソースタグを使用してデータエクステンションから JSON データを読み込み、そのデータのフィルターおよび並び替えを行います。
1{{.datasource JsonVar type=variable maxrows=2}}
2 {{.data}}
3 {
4 "target":"key:JsonData.JsonData",
5 “filter" : "EmailAddress == EmailAddr",
6 “order" : [ "EmailAddress ASC", "[_CustomObjectKey] DESC" ]
7 }
8 {{/data}}
9 Email Address: {{emailaddress}}
10 Region: {{region}}
11 State: {{STATE}}
12 City: {{JSONVar.City}}
13{{/datasource}}この例では、次の情報が生成されます。
1Email Address: john@example.com
2Region: West
3State: California
4City: San Francisco1Email Address: carla@example.com
2Region: Central
3State: Indiana
4City: Indianapolisネストされるデータソースの例
次の例では、データソースタグを使用して、ネストされる JSON から情報を取得します。
1%%[ var @Json set @Json = ' [{ "emailaddress": "john@example.com", "Region": "West", "State": "California", "City": "San Francisco", "PhoneNumbers": [{ "Type": "Home", "Number": "555-555-1111" }, { "Type": "Cell", "Number": "555-555-2222" }]}, { "emailaddress": "carla@example.com", "Region": "Central", "State": "Indiana", "City": "Indianapolis", "PhoneNumbers": [{ "Type": "Home", "Number": "555-555-4444" }, { "Type": "Cell", "Number": "555-555-5555" }]}]' ]%%
2
3{{.datasource JSONVar type=variable maxRows = 20}}
4 {{.data}}
5 { "target" : "@Json" }
6 {{/data}}
7 Email Address: {{emailaddress}}
8 Region: {{region}}
9 State: {{STATE}}
10 City: {{JSONVar.City}}
11 {{.datasource JSONPhone type=nested maxRows = 10}}
12 {{.data}}
13 { "target" : "JsonVar.PhoneNumbers" }
14 {{/data}}
15 {{JSONPhone.Type}}: {{JSONPhone.Number}}
16 {{/datasource}}
17{{/datasource}}この例では、次の情報が生成されます。
1Email Address: john@example.com
2Region: West
3State: California
4City: San Francisco
5Home: 555-555-1111
6Cell: 555-555-22221Email Address: carla@example.com
2Region: Central
3State: Indiana
4City: Indianapolis
5Home: 555-555-4444
6Cell: 555-555-5555