Help with API response and value identification

Hi guys,
I have a server action API call that returns something like the following

{
  "$total": 2,
  "$page": 1,
  "$items": 1,
  "$resources": [
    {
      "$key": 1,
      "$title": "Test",
      "myid": 1,
      "mytext": "Test",
      "myreference": "123"
    }
    {
      "$key": 2,
      "$title": "Test Again",
      "myid": 2,
      "mytext": "Testing 234",
      "myreference": "234"
    }
  ]
}

I have a repeat on $resources in a table.
the myid, mytext and myreference, dependant on the endpoint may be completely different titles, but I will always want to bind client side what is returned in those 3 property paths (values 3-5)

Any ideas would be appreciated.
Thanks

I don’t know how to help you in a Wappler context, but if you can run JavaScript code you can use Object.keys() and Object.values()

let x = {
      "$key": 1,
      "$title": "Test",
      "myid": 1,
      "mytext": "Test",
      "myreference": "123"
}
let keys = Object.keys(x) // Array(5) [ "$key", "$title", "myid", "mytext", "myreference" ]
let values = Object.values(x) // Array(5) [ 1, "Test", 1, "Test", "123" ]

And then you access the elements based on their index instead of name, e.g.:

keys[2] // "myid"
values[2] // 1

Indexes start counting from 0, so it’s 2-4 instead of 3-5

You can run that example in your browser’s dev console just to play with it. When you’re ready you can search on the forums how to access Wappler variables from regular Javascript code. Maybe you could make a Javascript code to create those properties you always know the name getting the values from their indexes, and in your table you bind to the property name you create. Though this sort of thing is probably dealt better directly in the server action, to rename those properties to something you always know the name (though I don’t know how to do that on top of my head), so your table always binds to the properties you know the names

You can try checking if Wappler has a formatter equivalent to Object.keys and Object.values to get an array from an object

Sorry if this whole text sounds confusing, I don’t have much time but just wanted to let you know about these possibilities

1 Like

Thanks @Apple, appreciate your help.
Does anyone know if/how I can do this server side with a repeat of the resources and outputting just the
required Object.keys[2] [3] etc

Can you try to do a Set Value with that inside the Repeat of $resources? I think you have to use the $value (of the Repeat) variable and apply the Keys or Values method on that variable

1 Like

Thank you @Apple
So I set a repeat on $resources and collection values api.data.$resources.values()
Then set values within the repeat $value.values()[4], $value.values()[5] etc