1

I am trying to set dynamic JSON parameters in an ampscript HTTPPost. When attempting to insert those parameters it only returns the actual ampscript function and not the data that was supposed to be inserted. In the code you can see both options I have tried, neither have worked.

%%[

var @payload, @response
set @fname = "Test"
set @lname = "Record"


set @payload = '{
   "Order Number":10110113,
   "First Name":"OutputLine(v(@fname))",
   "Last Name":"%%=v(@lname)=%%",
}'

set @request = HTTPPost("https://httpbin.org/post","application/json", @payload, @response)

]%%
3

I believe i can only give a partial information because my solution may produce a follow up mistake that Gortonington solved in a different case but i couldn't find it right now. What i believe you have done wrong:

First of all: You are inside a ampscript code block, therefore the example you have chosen to do with lastname cannot work because you never ended the codeblock you started at the beginning, but start a inline codeblock.

Second: You are inserting the ampscript function as text it therefore it will not work.

%%[

var @payload, @response
set @fname = "Test"
set @lname = "Record"

// i believe this does not work because it does break the string at the second ', therefore gortonington did something with ~ and replaced it afterwards. 
set @payload = '{
   "Order Number":10110113,
   "First Name":"~v(@fname)~",
   "Last Name":"~v(@lname)~",
}'
set @payload = Replace(@payload,'~',"'")

// second example which should work
set payload = Concat('{"Order Number":10110113,"First Name":"',@fname,'","Last Name":"',@lname,'",}')

set @request = HTTPPost("https://httpbin.org/post","application/json", @payload, @response)

]%%
2

EDIT I just noticed Johannes answer, his is correct so please upvote/mark his answer, not mine - but I am leaving mine here for added detail into the issue


Your issue comes in that you are setting the functions inside of your JSON as strings. By putting them inside of an AMPScript var, you declare them as strings and they are not processed.

I would try the following:

%%[

var @payload, @response
set @fname = "Test"
set @lname = "Record"


set @payload = CONCAT('{
   "Order Number":10110113,
   "First Name":"', @fname, '",
   "Last Name":"', @lname, '",
}')

set @request = HTTPPost("https://httpbin.org/post","application/json", @payload, @response)

]%%

I put your JSON string into a CONCAT which will allow you to add AMPscript var and strings together. Notice that I also removed the v() and OutputLine() functions as they won't be necessary inside the AMPScript Block.

Note that you can store AMPscript functions inside of AMPscript vars and execute them by using TreatAsContent() function, but I do not think that would work well here.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.