Reputation: 3327
I'm trying to format the output from my aws api but can't get one of the lists in the object to come through. I have the data loaded in dynamodb and my data looks like this:
[
{
"lastname": "person1",
"firstname": "last2",
"employeeid": 7,
"hrs": [
{
"availhrs": "16",
"ttlhrs": "12",
"bllhrs": "14",
"rto": "16",
"ondeckhrs": "0",
"nonbllhrs": "0",
"employeeid": 78,
"dtmonth": "2016-01-01T00:00:00.000Z",
"clientid": 1,
"clientId": 1,
"employeeId": 78
},
{
"availhrs": "16",
"ttlhrs": "17",
"bllhrs": "15",
"rto": "26.5",
"ondeckhrs": "0",
"nonbllhrs": "0",
"employeeid": 78,
"dtmonth": "2016-02-01T00:00:00.000Z",
"clientid": 1,
"clientId": 1,
"employeeId": 78
}
]
}
]
I try to format it like this:
#set($inputRoot = $input.path('$'))
{
"employees": [
#foreach($elem in $inputRoot.Items)
{
"employeeid": "$elem.employeeid.N",
"lastname": "$elem.lastname.S",
"firstname": "$elem.firstname.S",
"hrs": [
#foreach($elem in $inputRoot.Items.hrs)
{
"availhrs": "$elem.availhrs",
"ttlhrs": "$elem.ttlhrs",
"bllhrs": "$elem.bllhrs",
"rto": "$elem.rto",
"ondeckhrs": "$elem.ondeckhrs",
"nonbllhrs": "$elem.nonbllhrs",
"dtmonth": "$elem.dtmonth"
}#if($foreach.hasNext),#end
#end
]
}#if($foreach.hasNext).#end
#end
]
}
but then my output looks like this:
{
"employees": [
{
"employeeid": "7",
"lastname": "last2",
"firstname": "person1",
"hrs": []
}
]
}
what am I doing wrong in the formating where my hrs part is not coming out?
Upvotes: 0
Views: 1034
Reputation: 1690
Your inner loop is not correct. You should do the iteration over $elem.hrs instead of $inputRoot.Item.hrs
#set($inputRoot = $input.path('$'))
{
"employees": [
#foreach($elem in $inputRoot.Items)
{
"employeeid": "$elem.employeeid.N",
"lastname": "$elem.lastname.S",
"firstname": "$elem.firstname.S",
"hrs": [
#foreach($hour in $elem.hrs)
{
"availhrs": "$hour.availhrs",
"ttlhrs": "$hour.ttlhrs",
"bllhrs": "$hour.bllhrs",
"rto": "$hour.rto",
"ondeckhrs": "$hour.ondeckhrs",
"nonbllhrs": "$hour.nonbllhrs",
"dtmonth": "$hour.dtmonth"
}#if($foreach.hasNext),#end
#end
]
}#if($foreach.hasNext).#end
#end
]
}
Upvotes: 2