cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Handing complex JSON objects

AMacourek
Deputy Chef III
Deputy Chef III

I have a need to read data from several sources and push that data into another API.  The API input is the following schema:

{
  "matchOnName": true,
  "entities": [
    {
      "id": "00000000-0000-0000-0000-000000000000",
      "entityFieldValues": [
        {
          "fieldId": "00000000-0000-0000-0000-000000000000",
          "number": 0.0,
          "string": "",
          "dateValue": "",
          "boolean": false
        }
      ]
    }
  ]
}

What I need to do is for each record from the source build a record to add to the entities collection. Before adding to the collection, for each value, I need to add a record the entityFieldValues collection.  This is trivial code in C#, JS, etc.  I just want to know how to translate it into Workato.

I guess I could create a Ruby action to handle this, but I am unclear how to create hash objects based on a sample schema. The approach would be to first create base object.  Then, for each source record, create array of values and call a function to add the values to the list for each value.  Then, take this array and combine to create the entity object and the add to the entities collection. Would these all be Ruby actions?

Also, I have a Recipe Function that returns an object result.  How can I pass this result to a Ruby action as an object?

 

6 REPLIES 6

rajeshjanapati
Executive Chef I
Executive Chef I

Hi @AMacourek, ratherthan going with ruby or js, i can suggest you one native to workato solution, which will help you out, you can select JSON Request Body in the HTTP step rather than Raw JSON Request Body.

When you choose JSON Request Body as the Request Content Type, you can define the schema based on your request payload. This allows you to easily map data pills from previous steps without writing formulas manually or scripting.

For your reference, I am attaching an image. With this approach, there is no need for any additional steps.

rajeshjanapati_0-1770788937038.png

 

AMacourek
Deputy Chef III
Deputy Chef III

I think that you did not understand my question.  I have a Recipe Function that returns a defined schema.  

image.png

After calling this function, I want to use the list and the data from another output to build a payload.  I figured out that I needed to define the schema of a list to be able to use the list from the output.  

Now, here is what I am trying to do.

  1. Create final object - { "matchOnName": true, "entities": [] }
  2. Loop through each record from source.
  3. Create a JSON object for the record - { "id": "00000000-0000-0000-0000-000000000000", "entityFieldValues": []}
  4. For each property that I want to add, generate use the list above to find the matching definition and build an object like:
    {
    "fieldId": "00000000-0000-0000-0000-000000000000",
    "number": 0.0,
    "string": "",
    "dateValue": "",
    "boolean": false
    }
    and add this object the entityFieldValues above
  5. Then, add the record for the object to the entities collection.
  6. Finally, send the JSON object to another API

 

rajeshjanapati
Executive Chef I
Executive Chef I

one question is, how do you want to make a request to API, either you want to pass whole json body with array of items at once or one item at a time?

please let me know, then I can able to suggest the specific solution.

The API call would be the entire payload with the array of items.