Array response Pack

My use case : a get request. I added to a pack schema and a sync table.

But I have a problem with the schema sync with this type of response :

{“things”:[{“thing”:{“id”:1,“name”:“super name”}},{“thing”:{“id”:2,“name”:“super name 2”}}]}

I don’t understand how I have to write the different schemas…

1 Like

Hi @Virginie_Beurton_Le_Mignon - Welcome to the Coda community! Schema design is certainly a bit tricky at first. For a response like this, you could write code like this:

const ThingSchema = coda.makeObjectSchema({
  properties: {
    // Use fromKey to rename "id" to "thingId", as per the best practices.
    thingId: { type: coda.ValueType.String, fromKey: "id" },
    name: { type: coda.ValueType.String },
  },
  displayProperty: "name",
  idProperty: "thingId",
});

pack.addSyncTable({
  name: "Things",
  description: "",
  identityName: "Thing",
  schema: ThingSchema,
  formula: {
    name: "SyncThings",
    description: "",
    parameters: [],
    execute: async function ([], context) {
      let response = await context.fetcher.fetch({
        // ...
      });
      // Pull out the outer list of "things".
      let things = response.body.things;
      let rows = [];
      for (let item of things) {
        // Reach into the inner "thing" and add it to the rows.
        rows.push(item.thing);
      }
      return {
        result: rows,
      };
    },
  },
});

There’s a lot going on there, so let me know if you have follow-up questions.

2 Likes

Perfect :slight_smile: Thank you !