@thechrishaddad was close, but not quite with his answer.
If you don't want to push the GTL variables to AMPscript, you can follow the example (with slight modification by me) from the documentation:
%%[ var @Json set @Json = ' {"emailaddress":"[email protected]","Region":"East","State":"North Carolina","City":"Charlotte"}' ]%%
{{.dataobject JsonVar type=variable source=@Json maxrows=20}}
{{.data}}
{"target":"@Json"}
{{/data}}
{{/dataobject}}
{{#jsonvar}}
Email Address: {{emailaddress}}<br>
Region: {{region}}<br>
State: {{state}}<br>
City: {{JSONVar.City}}
{{/jsonvar}}
This will output your GTL data as required.
Now, if you do want to push the GTL variables into AMPscript variables, I have updated @thechrishaddad's code to display a working version with correct syntax:
%%[
SET @bar = '{ "First Name" : "Angela", "Last Name" : "Cruz", "City" : "Chicago"}'
]%%
{{.datasource details type=variable source=@bar}}
{{.data}}
{"target":"@bar"}
{{/data}}
%%[
SET @FirstName = TreatAsContent('{{[First Name]}}')
SET @LastName = TreatAsContent('{{[Last Name]}}')
SET @City = TreatAsContent('{{City}}')
]%%
Hello %%=V(@FirstName)=%% %%=V(@LastName)=%% from %%=V(@City)=%%!
{{/datasource}}
Notes on changes:
- The
{{/datasource}}
needs to below the output if you want to run this similar to a FOR loop and output multiple rows/objects in your JSON. (As there is only 1 row/object on this sample, it is not required, but wanted to note for any future needs.)
- All GTL variables with whitespace in their names requires
[ ]
around them.
- In order for AMPscript to recognize GTL variables, you need to wrap them in a
TreatAsContent()
function.
"
char, you are using the special characters“
and”
which can cause syntax or other errors.