Accessing Nested Objects

Hey! I’ve gotten everything working with the API as far as fetching data, but I’m running into an issue with getting data that’s nested inside that data.

The Coda API will allow me to pull in a table, separated by rows. Ultimately, my goal is to have each row on its own page, with the cell data from that row populating the fields on that page.

The actual cell data, though, is inside an object (‘values’) that’s nested inside the object for each row. The row data itself pulls in the rows nested under the object ‘items’:

Using get(‘items’) in the Key field of the API, I’m able to pull in just the array of rows.

On the index-type page, I can populate the data just fine:

But when I try to do it for the individual pages, it always shows the ‘values’ data for the first row. I’ve checked, and the slug is updating appropriately (i.e. the slug for “Test Name 2” is the right one for that page), but the data on the page itself is still the data from “Test Name 1”.

I selected my Coda data as the source on a page level for the single-row page, but I can’t do an iterable because I’m not trying to show all of the data (just the data for that page), and I’m not sure how else to drill down into the nested object.

Is there another way to access the nested object for each of the original objects, either with another API key or with a function in the builder? Thanks in advance!

@chelce

Let’s go step by step.

Can you first make new GET request in Divhunt with endpoint that is able to get single item from Coda?

So you need to have 2 GET requests,

  • One for fetching all items.
  • One for fetching single item.

Once you have that ready, we can move on.

Okay, I set up two different GET requests, one that lists the rows in the table, and one that gets only the first row using that row’s id as part of the endpoint. What’s next?

@chelce

You need to use Dynamic properties, this one is bit tricky since we don’t have yet documentation for that.

So inside request for single item, scroll down and you will see properties, configure as following:

So the key is id
and value is i-4c7jo9yqfn

i-4c7jo9yqfn will be default value so it always fetches something instead of nothing.

Set that and save.

Then in the URL section instead sending hard-coded ID, send it as variable ${properties->get(‘id’)}

So instead of
…/grid-0WWlxApqSN/rows/i-4c7jo9yqfn

Set
…/grid-0WWlxApqSN/rows/${properties->get(‘id’)}

1 Like

@chelce

Just to mention that when using “Variable” feature in REST API, the data get’s stored in variable, but every time you refresh website, it will take longer time to get those data so it’s slower.

However, I just deployed some improvements for that, so you now should experience much much faster admin panel even with using “Variable” features.

1 Like

@dejan

Awesome to hear about the speed!

I just tried the properties fix, and it doesn’t seem to translate to the URL (the URL reads it as ‘null’).

curl --request GET 'https://coda.io/apis/v1/docs/2rajVncYaR/tables/grid-0WWlxApqSN/rows/null'

What am I doing wrong? And thank you for your help. :slight_smile:

image

Those single quotes needs to be straight and not curved
Probably when copying/pasting on forum it got changed.

Perfect. Fixed it. What is next?

Now create new page (or use existing one) and right click on that page > set source > and then choose that GET request you made

image

And for the last step for this to work, you need to configure proper route for page, in this case, we are using dynamic property id, so you need to configure route to be for example /something/:id

When source is configured, all the data for that single page are accessible using variable modal system under page > data keys.

Try that and let me know if works for you.

I was able to set up the index page using the data from the GetAll API request, and then I set up the singular page using the data from the GetOne API request. I ran into an issue when I tried to link the two. The method of clicking on “Page” > “name of the singular page” didn’t work, BUT I was able to direct it to a link instead. I entered the URL for the singular page, and adding variable /${value->get(‘id’)} worked to direct it to the right spot.

One final question on this: is there any way to control the sorting of the elements in the GetAll request? Not a big deal if there isn’t, but I just thought I would ask.

Thank you so much for all of your help! You have saved me a lot of time and frustration. :slight_smile:

@chelce

You can still link to page in Settings tab, and then go to Advanced tab and you will see the that same ID property waiting for you to fill up with value (also you will have variable modal) so it’s easier for you.

@chelce

Only way to sort in this case is with request it-self, so update the URL endpoint or add query parameters that Coda allows for filtering.

You can also do following for example

  1. Add one more dynamic property sort for example, and se default value let’s say desc (this needs to match Coda API sorting docs)
  2. Update route to be /something/:id/:sort
  3. Now you can have multiple sorting using different urls for example /something/id/desc or /something/id/asc

So that’s how dynamic properties work.

You can also pass additional variables in route, let’s say if items has “name” you can do something like /something/:id/:name and then configure both id and name form Advanced tab, setting the name variable with function ->slug(), if you haven’t seen, in modal where you choose variables, you can add functions as well, and slug function converts string to slug, eg “This is name” to “this-is-name” so you get pretty page urls.

That’s amazing. Thank you so, so much!