Can unexpected JSON responses to a REST model query be stored in a text field on that model?

  • 1
  • Question
  • Updated 10 months ago
  • In Progress
I've been using a REST model with two sets of fields to post a request to an API and receive a response, and storing the response data in fields in the same model works - when the response is properly formatted.

However, the API I'm testing sends the fields back as nested objects, and Skuid doesn't have a data field type to properly parse that into. My instinct was to see if I could store the entire response in a text field, and then parse it afterwards with a snippet, but I'm receiving a JSON upload error when the response comes in, and nothing gets stored.

Any ideas?
Photo of mB Remi Gelinas

mB Remi Gelinas

  • 374 Points 250 badge 2x thumb

Posted 10 months ago

  • 1
Photo of Mark DeSimone

Mark DeSimone, Official Rep

  • 11,050 Points 10k badge 2x thumb
Hello Remi,

Can you share details for the error you see when the response comes in, or any more information about what's going wrong? I believe you will need to parse the response with scripting as you suspect, but it sounds like the first step is to try and resolve the error. 
Photo of mB Remi Gelinas

mB Remi Gelinas

  • 374 Points 250 badge 2x thumb
Hey Mark, on a hunch I tested the batch requirement for the API, and I was able to pinpoint the cause of the error. However, I'm unsure how to circumvent it.

The API expects rows to be formatted as an array of objects in the JSON payload, whereas Skuid either sends rows individually or, when the batch send option is enabled, sends them as nested objects tied to the row Id.

Is there a way to specify JSON payload format (perhaps through a snippet?) before the model sends the query?
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Remi, re: this comment: "However, the API I'm testing sends the fields back as nested objects, and Skuid doesn't have a data field type to properly parse that into."

Actually, Skuid should be able to handle nested objects and parse them into appropriate data field types. Just use "." in your field names to delineate nested object boundaries. Example, let's say that your REST API returns an array of data rows like this:

[
    {
        "firstName": "Sally",
        "lastName": "Smith",
        "address": {
            "city": "Phoenix",
            "state": "AZ",
            "zip": "85021"
        }
    },
    {
        "firstName": "Myasia",
        "lastName": "Harvey",
        "address": {
            "city": "Middlebury",
            "state": "VT",
            "zip": "05472"
        }
    }
]


firstName and lastName are easy - but let's talk about the "address" field. What you can do is to add a separate field for each "nested" portion of the address, separated by a "." in the name. So you'd have the following fields in your Model:

"firstName"
"lastName"
"address.city"
"address.state"
"address.zip"

Skuid will interpret the "." in the field names as a nested object boundary.
Photo of mB Remi Gelinas

mB Remi Gelinas

  • 374 Points 250 badge 2x thumb
Thanks for the reply Zach, that's actually really interesting - but my use case requires manipulating the format before the POST is sent.

For example, Skuid initially sends single rows of data to the API in the following format:

{
  "field1": 1,
  "field2": 2,
  "field3": 3
}

Even when the batch send option is turned on, the format becomes this, associated with row ID:

{
  "sk-3zb6SA-91": {
    "field1": 1,
    "field2": 2
    "field3": 3
  }
}

The API I'm trying to integrate expects a format like this, representing an array of rows:

[
  {
    "field1": 1,
    "field2": 2,
    "field3": 3
  }
]

Is there any way to specify that Skuid should format the rows as such before the POST request is sent?
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Remi, yes, you can do define a custom "payload transformation snippet" that allows you to send the data in whatever format your API expects. Here's how you do this:

1. Create a new Inline JavaScript Snippet called "PayloadTransformation" or something like that, with the following contents:

return skuid.$.map(arguments[0].changes, function(changedFields, rowId) {
   return changedFields;
});


2. In your REST Model's "Insert" method, select the following:

URL expects batch inserts: true
Send new field values...: As custom payload format in request body
Payload Generation Snippet: enter the name of the snippet you just created

That should do it!