Filter the results from an API search

Asked Jul 14, 2022·2 replies·Last activity 4 years ago·Open in Discord ↗
Archived from the original community forum
Things may have changed since it was written. Still stuck? Ask the community on Discord.

I've being tryng to make a command that uses this Tarot API:
https://rws-cards-api.herokuapp.com/

the api itself -> https://rws-cards-api.herokuapp.com/api/v1/cards

I want to make something like !tarot Death and get the "meaning_up" from the result of the urlfetch https://rws-cards-api.herokuapp.com/api/v1/cards/search?name=Death
but I don't know hot to get only that field to show up as a response from the command.

I've tried things like this:

or things that look like this
```$(eval JSON.parse(decodeURIComponent($(urlfetch json https://rws-cards-api.herokuapp.com/api/v1/cards/search?name=$(querystring).cards.0.meaning_up||`Error!`)```
but I still can't figure out how to do it.
This is my first time trying to do that kind of commands using jsons, APIs and urlfetch
I'll appreciate any advice in the right direction

2 replies

Hey @Sandy Mouse!

You were kinda close with your second example of what you tried, but when the key is a number or a string containing anything else than letters and underscores, use square brackets []: [2] or ['hello-world']

Without further ado, here's how you reach the meaning_up key:

$(eval const response = $(urlfetch json https://rws-cards-api.herokuapp.com/api/v1/cards/search?name=$(querystring)); response.cards[0].meaning_up)

And with some error handling:

$(eval const response = $(urlfetch json https://rws-cards-api.herokuapp.com/api/v1/cards/search?name=$(querystring)); '$(query)' ? response.cards && response.cards.length > 0 ? `${response.cards[0].name}: ${response.cards[0].meaning_up}` : 'API response incomplete.' : 'Please specify which card you want to know the meaning of.')

Thank you!
I really appreciate your help, I could't figure out how to do that part.