Can't display my json response

Hello,
i’m a newbie in pack and also in Javascript, sorry :weary:
Could someone can help me to display the result of my API call ?

I managed to build my package until I got the result of my API call with a parameter.
But no matter how hard I try, I can’t get it to display in my document.

If you have any tips on how to get me out of this, it would be really nice. Thanks

Response schema :

{
“name”: “peter”,
“gender”: “male”,
“probability”: 0.99,
“count”: 165452
}

API use :
api.genderize.io

I enclose a screenshot with the document on the left and the code for my pack on the right.

1 Like

Hey Julien! Welcome to the fun that is Pack Studio! There are many on here more advanced than me but I am happy to try to assist. It looks like you need a line to to define which schema should be used for the object being returned. The Coda guide is stellar (Schemas - Coda Pack SDK) and it advises something like this

pack.addFormula({
  // ...
  resultType: coda.ValueType.Object,
  schema: MySchema,
  // ...
});

You can then tweak what you want to see as the display property or do any editing prior to returning the response etc.

Hope that helps some!

2 Likes

I just did a quick build and was able to return “male” for my name using the below. I did simplify the schema to just gender and made it the display property.

Screen Shot 2022-06-27 at 7.18.10 PM

3 Likes

Hi @Julien_BOIDROU - @Thomas_Baucom has provided some good advice, although looking at your screenshot it appears you are already setting the resultType and schema correctly. Could you include a screenshot of how you are using the formula and what the result is?

3 Likes

What data do you want the Pack formula to return and show in your doc? Just the text “male” or “female”? Or the whole object with probability etc?

3 Likes

Thank you very much for your messages!

I finally found a solution by changing the result type to String instead of Object :

I get the result in json format and then I parse my different retrieved information


The next step is to find an easier way to integrate the package without the need to use the “parsejson()” function.

The idea would be to have a column dedicated to the pack with only one argument (the first name). And then to be able to associate “Add” buttons to the different information retrieved. Like for the Twitter pack :arrow_heading_down:

I don’t know if I should use another method…

If you have any ideas or examples I’m interested :sweat_smile:

To be continued

1 Like

Hmm, you were on the right track with the first one. To avoid ParseJSON(), you need to return an object withe a schema, not a string.

Could you paste your original code here? Perhaps if we can load it into our editor we could find the bug. (Nothing jumps out at me immediately looking at your initial code - it seems quite close to correct).

2 Likes

Hi Nick, thanks
Yes i need to change the method.
My actual code:

import * as coda from "@codahq/packs-sdk";
export const pack = coda.newPack();
pack.addNetworkDomain("api.genderize.io");
// Add column
pack.addColumnFormat({
  name: "Genderize",
  instructions: "Enter name in this column",
  formulaName: "Gender",
});

// Define Formula Name
pack.addFormula({
  name: "Gender",
  description: "Find person gender",
// Define paramaters
  parameters: [
    coda.makeParameter({
      type: coda.ParameterType.String,
      name: "name",
      description: "add the name",
      optional: true,
    }),
  ],
  resultType: coda.ValueType.String,
  execute: async function (
    [name],
    context,
  ) {
    let url = "https://api.genderize.io";
    url = coda.withQueryParams(url, {
      name: name
    });
    let response = await context.fetcher.fetch({
      method: "GET",
      url: url
    });
    let data = response.body ;
    return data
  },
});
1 Like

Is this what you’re aiming for?

It’s mostly what you already had in your first post… but here’s what I changed vs your latest code:

  • Added schema
  • Within that schema, declared a display property to control what’s shown on the chip
  • set resultType back to Object, and set Schema in formula

The result is that the formula returns an object, with chip saying “male” or “female”, with other properties accessible inside it. Is that what you’re looking for?

4 Likes

Hi Nick. Thank you so much for taking the time to help me. Yes, that’s exactly what I wanted to do. Now I just have to study all this quietly to understand how you did it. It’s really great, thanks again.

1 Like

For sure! Let me know if there are other things you’d like to do as well that you need help with.

For example

  • the column format might be a bit weird right now, because people will input the name, but then see just the gender (and not the name anymore). We could set it up so that it shows the name on a chip and the gender is accessible from the chip / “Add column” button. Or chip could be labeled “Julien (male)” or something.
  • I saw you can feed nationality to the API to get more accurate results (though you’d want to fall back to generic if not enough results for an answer)
  • gracefully handle errors such as name not being found

(Or none of this! It’s cool as-is and a great first Pack)

3 Likes

yes I just tested to crash it and I proceeded to change the default property displayed.

now I display the first name with an icon it looks much better.

adding the nationality parameter is actually the next step which seems a little bit tricky.

It’s by doing that we learn so I’ll try to find solutions but it’s true that the initial objective is achieved with this version. :pray::relaxed:

2 Likes