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

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?

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. 

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?

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.

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?

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;
});

  1. 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!