How to Access a Value from Key Value array returned in Server Connect

I could swear I've done this before but can't for the life of me figure out how. Anywho...

I have an API call in a server request that returns exchange rate data like this...

 "data": {
      "data": {
        "AUD": 1.5954202845,
        "BGN": 1.8374102141,
        "BRL": 5.8846708234,
        "CAD": 1.4395702387,
        "CHF": 0.889800103,
        "CNY": 7.2634507878,
        "CZK": 23.5735438441,
        "DKK": 7.0209012078,
        "EUR": 0.9408001165,
        "GBP": 0.7817701284,
        "HKD": 7.7697814779,
        "HRK": 6.6180606635,
        "HUF": 375.3047294933,
        "IDR": 16424.190094376,
        "ILS": 3.6238005279,
        "INR": 87.2032291999,
        "ISK": 137.6289139839,
        "JPY": 149.8041957748,
        "KRW": 1451.6047592276,
        "MXN": 20.5372327795,
        "MYR": 4.4539506337,
        "NOK": 11.0890822173,
        "NZD": 1.766710251,
        "PHP": 57.5633869219,
        "PLN": 3.9116205264,
        "RON": 4.6839307281,
        "RUB": 89.6462648221,
        "SEK": 10.4124618261,
        "SGD": 1.3378301405,
        "THB": 33.6367943783,
        "TRY": 36.4333356746,
        "USD": 1,
        "ZAR": 18.4604632239
      }
    }

I can access a particular rate using something like {{api.data.data.AUD}} but I need to be able to use a variable for the actual currency code.

Any help would be most appreciated! Thanks.

Have you tried where?

This most likely isn't correct, but something like this

data.data.where('$key',yourvariable,'==')[0].value

or this, can't recall if it's value or $value

data.data.where('$key',yourvariable,'==')[0].$value

You could try with a hardcoded value first
data.data.where('$key','AUD','==')[0].value

You may not need the .value as well. Can't remember when it's needed. So you could also try.

data.data.where('$key','AUD','==')

Thanks for the ideas. Unfortunately they all just return null. I did change the syntax around as I believe it's {{api.data.data.where('$key', '==', 'AUD')}}

Edit: Acutally I had turned the output for the API off. Oops. Without the [0].value or $value it returns the full data set. Otherwise it returns null

Can you provide a screen from dmx.app.data of the server connect?

Also, make sure the operator is on the right and value in the middle.

But the operator doesn't go on the right.

I'm just running it in the browser.

This is the output...

Can you use code view and change it to see if it makes a difference? When I started using .where() all references in Wappler Community had the operator on the right. That is how I code it for all my where clauses.

Same result. Weird that switching those up doesn't make a difference.

Actually that changes it. Are you sure yours are the other way around?

All of mine have operator on right. I don't use the UI to create them.

Labels.data.query.where('LabelId','INBOX','==')[0].ThreadsUnread

Some are quite complex

Emails.data.query.where('$index', nextThreadIndex, '==')[0].GmailLabelIds.parseJSON().where('$value','UNREAD','contains').values('$value')=='UNREAD'

Have you tried doing it without the where?
api.data.data.AUD

Yes...

Are any of yours from an API source? Also, are those server side? They look like they're client side.

I can do this with a db query in the server connect and it works fine..
{{query1.where('category_id', '==', 1)}}

Indeed Client Side is the other way around...
dmx-bind:repeat="sc_tripbits.data.tripbits.where('category_id', category_id, '==')"

Are you trying this server side?

Apologies, I thought this was being done on the client side.

Looks like you are saying this returns the value -- {{api.data.data.AUD}}. Is there not a way to replace the AUD with an expression or setValue?

Something like this?
api.data.data.$_GET.currencycode

Yeah - it's all server side.

The currency code is in a variable. I've tried various syntax like api.data.data.currency_code
api.data.data + '.' + currency_code

but can't seem to find the secret sauce.

I've now created a db table which I overwrite every time with the new exchange rate values and then use that data. It works but is just kind of an annoyingly inelegant sollution as there must be some way to access those values directly!

Thanks for your help.

{{api.data.data[variable_name]}}

Use square brackets instead of dot-notation to use variables

1 Like

Would it still need a dot after the second data?

{{api.data.data.[variable_name]}}

I based on her statement {{api.data.data.AUD}} works, so I simply replaced the AUD part with square brackets

If we removed the second data, it's likely it would break. This is specific to her JSON object that also seems to have a "data" property.

I was just commenting on your example. It is not showing the . after the second data.

So this
{{api.data.data.AUD}} isn't the same as this {{api.data.data[variable_name]}}

Ah, no, you don't put a dot when using square brackets

It's like array indexes, row[0], row[variable], row[$index]

1 Like

THANK YOU!!!! I knew there was a way to do this I just couldn't for the life of me remember how! Off to pretty up my code. :grinning: