I have an array of objects $arr
and an object has a property named as jsonData
which contains json data in a string, how to parse that string to actual JSON object and retrieve lets say value for key name
? I tried this:
#foreach ($obj in $arr)
#set ($jsonData = "#evaluate(${obj.jsonData})")
$jsonData.get("name") ## <-- not working
#end
g.o.a.t, your answer is almost correct; there's a slight typo in that you want to use
$util.parseJson()
, not$utils.parseJson()
. Even so, it helped me tremendously, so thank you. I would have commented on your post, but I don't have enough rep. :(This is mainly AWS lambda (in python) response template related.. so.. if you want to return custom response code with json it is easy to return JSON in node js but when it comes to python as per i know, we cant raise dict as a value. So.. this may help. If you are doing like..
It will throw exception in errorMessage key and value as a string.
To overcome this..
Add response code 400 in Method Response
regExp in Integration Response :
Add Body Mapping Template as application/json:
Make sure you are not using single quotes in value in exception string.
Wrong:
Right:
I know it is not best solution... but this is the only solution i have found. Feel free if you have better solution.
If anyone using velocity in AWS API Gateway ends up here, you can use AWS'
$util.parseJson() to covert a string to JSON.
Make sure you note if your string is using single or double quotes. You may need to use $util.escapeJavaScipt.replaceAll() before parsing the string.
Fixed it like this:
I was using
velocity 1.7