get last attachment ID when returning {httpresponse} with Butler?
Asked by Kalvin Lyle:

I'm trying to get the last attachment on a card in Butler. When I GET the card attachments I can't seem to parse the JSON to get the last attachment.

I'm using this automation:

when an attachment is added to a card by anyone, get url "[https://api.trello.com/1/cards/{cardid}/attachments/?key=\[KEY\]\&token=\[TOKEN](https://api.trello.com/1/cards/{cardid}/attachments/?key=[KEY]&token=[TOKEN)\]", and set the card's description to "{httpresponse}"

The JSON result pastes into the description nicely, but anything I do to try to parse it returns an INVALID JSON.

Any ideas?


Answers:
author: milynnus
body: [@Kalvin Lyle](/t5/user/viewprofilepage/user-id/4845873)

{httpresponse} is probably a list of dictionaries. So you might need {httpresponse}\[-1\].url ... Try it. If I have time I will investigate further.

comments: author: Kalvin Lyle
body: the array is within the {...}, but {httpresponse\[0\]} does not work.  

The returned JSON structure looks like this:

\[  
{  
"id": "",  
"bytes": ,  
"date": "",  
"edgeColor": "#",  
"idMember": "",  
"isUpload": ,  
"mimeType": "",  
"name": "",  
"previews": \[  
{},  
{},  
{}  
\],  
"url": "",  
"pos": ,  
"fileName": ""  
}  
\]


author: milynnus
body: [@Kalvin Lyle](/t5/user/viewprofilepage/user-id/4845873)

It seems that you can get to an array for an item in a dictitionary but not to a dictitionary inside an array/list aka a list of dictitionaries.

<https://help.trello.com/article/1203-issuing-http-requests-with-butler>

Having said that you can use http request to an endpoint that will provide you what you need.

Here I have an example for attachments :

<https://github.com/xu2xulim/Superhero/blob/master/Superhero%20on%20deta/Micros/Attachments.md>

Currently it has only one endpoint.


author: milynnus
body: [@Kalvin Lyle](/t5/user/viewprofilepage/user-id/4845873)

If you are testing out the endpoint you should be using {cardidlong} instead of {cardid}


author: milynnus
body: [@Kalvin Lyle](/t5/user/viewprofilepage/user-id/4845873)

Updated the endpoint with the following payload (an example) :
> {  
> "card_id": "{triggercardidlong}",
>
> "alt_card_id": "{newcardidlong}",
>
> "option" : ""  
> }

where option can
> "" - all attachments,
>
> "first" - the first attachment added on the source card or
>
> "last" - the last attachment added on the source card


author: Kalvin Lyle
body: All the magic is happening in Butler. This seems to work now:

when an attachment is added to a card by anyone, get url "[https://api.trello.com/1/cards/{cardid}/attachments/?key=\[KEY\]\&token=\[TOKEN](https://api.trello.com/1/cards/{cardid}/attachments/?key=[KEY]&token=[TOKEN)\]", and set the card's description to "{httpresponse\[0\].name}"


author: Kalvin Lyle
body: Hmm scratch that, it works sometimes...


author: milynnus
body: [@Kalvin Lyle](/t5/user/viewprofilepage/user-id/4845873)

That's good news. Will you be trying to copy the attachment over or just to get the name.

Do you know if the last attachment is a cardlink or boardlink, will the above still work. In my work, I need to filter those out since they are part of the returned array of attachments.


author: Kalvin Lyle
body: My plan was to set the card cover by getting the last attachment id using {httpresponse} but this seems not possible using Butler. I can get the last attachment by getting {httpresponse.Badges.Attachments} from the card to get the length, but it doesn't seem possible to subtract 1 from that number to get the last attachment.


author: milynnus
body: [@Kalvin Lyle](/t5/user/viewprofilepage/user-id/4845873)

Got it to work

![Screen Shot 2022-04-01 at 6.36.42 PM.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/191104i7E9EA04C49185ED3/image-size/large?v=v2&px=999 "Screen Shot 2022-04-01 at 6.36.42 PM.jpg")


Tags: automation

butler