test
test

Reputation: 127

Add variable JSON with apache velocity

I need to able to dynamically add a variable to a JSON string, then escape it. Is it possible to do this with VTL?

Here is my case

#set( $foo = "bar" )
#set( $input = $util.escapeJavaScript( '{"foo": $foo }' ))

Where $foo would be the dynamically added variable to the JSON string i want to escape.

Upvotes: 1

Views: 1871

Answers (1)

test
test

Reputation: 127

I couldn't explicitly add variables to the JSON but a work around was to create an array of objects, where I would manually escape dynamically added object, as well as adding the incoming body JSON.

In my case it was

#set( $body = $util.escapeJavaScript( $input.json('$') )) 
{
    "input": "[$body, {\"id\":\"$context.requestId\" }]"
}

Upvotes: 1

Related Questions