1

we have a json object in a data extension. I would like to populate subject line depending on a field in that json.

How can I assign an ampscript variable a value from the json?

I tried this but that doesn't work:

%%[
var @Json 
set @Json = [order] 
]%%
{{.dataobject JsonVar type=variable source=@Json maxrows=1}}
{{.data}} 
{"target":"@Json"} 
{{/data}}

%%[
var @order_status
set @order_status = {{orderStatus}}
]%%
{{/dataobject}}

1 Answer 1

2

There are a couple of issues here.

Firstly, I would use datasource rather than dataobject as dataobject defines a global scope and while it makes the data available for reuse multiple times, the tags will need to be contained in section tags.

Secondly, you need to use the TreatAsContent() AMPscript function.

This should work:

%%[
var @Json 
set @Json = [order]
]%%

{{.datasource JsonVar type=variable source=@Json maxrows=1}}
{{.data}} 
{"target":"@Json"} 
{{/data}}

%%[
var @order_status
set @order_status = TreatAsContent('{{orderStatus}}')
]%%
{{/datasource}}
1
  • and what if you have a JSON with several objects and want to target a specific piece of the JSON? So for example when you have the follwing structure and want to target the second one?: [{ "test1": "1", "test2": "false" }, { "test1": "2", "test2": "true" }]
    – Martijn e
    Jan 4, 2023 at 11:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .