Get Array with no fieldId

MansourMansour ✭✭✭✭
edited June 24, 2019 in Questions
Trying to integrate Hubspot with Skuid Native but having issues pulling the following array into fields.
{
    "results": [
        25054251,
        25129831,
        25284327,
        25370947,
        25371816,
        25402825,
        25515091,
        25974488,
        27511859,
        28085871
    ],
    "hasMore": true,
    "offset": 28085871
}

There is no FieldId for me to store the data to the results into individual rows.  

Has anyone had success in pulling these types of arrays into Skuid?

Comments

  • edited June 15, 2019
    This is a good place to use a custom Response Parsing Snippet. You could use the Response Parsing Snippet transform the inbound results array of numbers into an array of objects that's more friendly to Skuid. 

    https://docs.skuid.com/latest/en/data/rest/#intercepting-response-payloads
  • MansourMansour ✭✭✭✭
    edited June 15, 2019
    Hi Zach,

    Thanks for pointing me in the right direction - are there examples of response parsing snippets I could see?

    Any help here would be much appreciated.


  • Aaron DearAaron Dear ✭✭
    edited June 20, 2019
    Hey Mansour - 

    Might look like this. I dunno which endpoint you're using, but your intercept payload probably looks something like the below:

    var params = arguments[0],
    $ = skuid.$;
    //grab the response body - this is what Skuid will use to build your records on this GET request
    var responseBody = params.response.body;
    console.log(responseBody);
    //store things for easiness
    var hasMore = responseBody.hasMore;
    var offset = responseBody.offset;
    var results = responseBody.results;
    //Intercept the results, adding an "id" to each number
    for (i = 0; i<results.length; i++){
        if(results[i]){
            results[i] = {"field_id" : results[i]};
        }
    }
    console.log(results);
    return results;
    All this does is add an object with a field_id property per array value.

    Your order of ops is:
    1. Add the js snippet. Note that if you change the snippet's name, it won't change the referenced snippet in the Response Payload snippet field.
    2. Check "Intercept Response Payload" then add your Response snippet. Skuid will run this snippet, then treat whatever it returns as the "real" payload.
    3. Click the model, then click "Add Field". The id of the field should be field_id - exactly matching your objects' key.
    4. Chuck it into a table and check it out. Bada-bing.
  • MansourMansour ✭✭✭✭
    edited June 24, 2019
    First of all, thanks a ton Aaron.  I think we're almost there.  I'm getting 
    [object Object] as the result from the field_id field you sent over. 

    Any idea on what I need to do to fix it?
  • Aaron DearAaron Dear ✭✭
    edited June 24, 2019
    For those in the audience, Mansour and I vidchatted this. His snippet was returning the field_id in an object, so he just needed to go down an object.

    That is, it looked like:
    field_id: {field_id: 120938213} 
    So in step #3 above, he made his id:
    field_id.field_id
    I didn't peek too hard at his setup, but here's how mine resolves with the above snippet. The left shows the table. The right shows the array representing the field_ids. If you're poking at this endpoint and are having the same issue, double-check your property's values for those iconic object brackets {}.



  • When I found this post I thought that I would be able to make some progress getting my API response call working also for the arrays included in the payload, but no luck for me...I would truly appreciate some guidance in how I can accomplish the similar as this original post.

    My data set has the array(s) of values without field name, similar to the original post, but they are nested in the payload and I cannot seem to retrieve the values. My understanding is that the dot notation (e.g. items.wmuauthors) ought to work to traverse in the hierarchy and be able to get these values a key/pair and turned into an object easier to deal with in Skuid (as Zach pointed out above).

    My json response looks like the following:

    {
        "items": [
            {
                "_archived": false,
                "_draft": false,
                "type": "77e1eaa40d1e6790e1b0649ea83725ea",
                "nameselectedtype": "Combustion and Flame",
                "pages": "466-483",
                "publicationid": "5f76cf2aef4962adf046a86f",
                "authors": "A. Schönborn, MD Le, R Fournet, PA Glaude, V Warth, B Sirjean",
                "name": "Auto-ignition control using an additive with adaptable chemical structure. Part I: Development of a kinetic model for 1, 3-cyclohexadiene and 1, 3, 5-hexatriene combustion",
                "volume": "205",
                "slug": "auto-ignition-control-using-an-additive-with-adaptable-chemical-structure",
                "wmuauthors": [
                    "5f76cf2aef49629a5c46a731",
                    "5f76cf2aef496274c646afcc",
                    "5f76cf2aef496253a646afc8"
                ],
                "updated-on": "2020-11-12T16:42:50.369Z",
                "updated-by": "Person_5d89d941ecc118d157384ca7",
                "created-on": "2020-11-11T14:25:42.586Z",
                "created-by": "Person_5d89d941ecc118d157384ca7",
                "published-on": "2020-11-12T16:42:50.369Z",
                "published-by": "Person_5d89d941ecc118d157384ca7",
                "createdby": "5f86563d1eeace2166c69ffe",
                "publisheddate": "2020-11-10T14:26:00.000Z",
                "rpas": [
                    "5f8999990a79f8934e564219",
                    "5f89997cc1898fcc0ed10a26"
                ],
                "tenure": "08c86bccc8851822db487e2acaeb5d07",
                "unsdgs": [
                    "5f8998f8e597a12643dff31b"
                ],
                "_cid": "5f97ef75ea6b048053ecfe68",
                "_id": "5fabf46699b8c34d93b0a834"
            },
     
            {
                and 9 more publications in between...
            }, 
    ],
    "count": 10,
    "limit": 100,
    "offset": 0,
    "total": 10
    }
    

    Below is my code in the simplest form (I want all values into a Skuid Model, but keeping it as simple as possible initially and expand when I get this working.)

    var params = arguments[0],
    $ = skuid.$;
    //grab the response body - this is what Skuid will use to build your records on this GET request
    var responseBody = params.response.body;
    //console.log(responseBody);
    
    var wmuauthors = responseBody.wmuauthors;
    console.log(wmuauthors);
    //console list "Undefined" - data set comprises of 10 records, all with values in wmuauthors
    
    //thus the following loop does not get started, "TypeError: Cannot read property 'length' of undefined"
    for (var i = 0; i<wmuauthors.length; i++){
        if(wmuauthors[i]){
            wmuauthors[i] = {"authorId" : wmuauthors[i]};
        }
    }
    console.log(wmuauthors);
    return wmuauthors;
    

    Worth pointing out is that I have "path to content" defined to items and the following gets displayed in the console when commenting out console.log(responseBody).

    I am no javascript guru (regrettably)....and after spending alot of time trying to make some progress it was time to ask the community...

  • Aaron DearAaron Dear ✭✭
    edited November 17, 2020

    Hey Erik,

    Your solution will look something like this:

    var params = arguments[0],
    $ = skuid.$;
    
    //your path to contents -- where "contents" means individual rows with properties
    let responseBody = params.response.body;
    let items = responseBody.items;
    
    //other values you might use in this snippet -- but aren't using right now. If you need extra transformation, it'll happen here
    let count= responseBody.count;
    let offset = responseBody.offset;
    let limit = responseBody.limit;
    let total= responseBody.total;
    
    //this is where Skuid picks up your records
    return items;
    

    Your code differs from Mansour's in that you don't need to add a field name (as far as I can tell) -- you just need to get the path to your rows sorted (items), followed by a return (return items;)

    Lemme know if that works.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!