Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse string into JSON

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
like image 908
Aamir Rind Avatar asked Sep 18 '25 19:09

Aamir Rind


1 Answers

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.

like image 199
g.o.a.t. Avatar answered Sep 23 '25 06:09

g.o.a.t.