• Documentation
  • Workato Blog
  • Product Blog
  • Return to Workato
  • Login
  • Automation Institute
  • Documentation
  • Community Forums
New Topic
Discussions Developer Community Connector SDK

Aalaap Ghag

started a topic over 2 years ago

Error: undefined method `to_hash' for #<Array:0x00007f6d738a0808> while capturing array as output fields

I'm trying to parse out an array response from an API call, but I can't figure it out. The API call is to https://jsonplaceholder.typicode.com/comments, which returns the following response:

 

[
  {
    "postId": 1,
    "id": 1,
    "name": "id labore ex et quam laborum",
    "email": "Eliseo@gardner.biz",
    "body": "laudantium enim quasi est quidem magnam voluptate ipsam eos\ntempora quo necessitatibus\ndolor quam autem quasi\nreiciendis et nam sapiente accusantium"
  },
  {
    "postId": 1,
    "id": 2,
    "name": "quo vero reiciendis velit similique earum",
    "email": "Jayne_Kuhic@sydney.com",
    "body": "est natus enim nihil est dolore omnis voluptatem numquam\net omnis occaecati quod ullam at\nvoluptatem error expedita pariatur\nnihil sint nostrum voluptatem reiciendis et"
  },
  ...
]

 

This is my connector code:

   

{
  title: "JSON Placeholder",
  
  test: lambda do
    get("https://jsonplaceholder.typicode.com/comments/")
  end,

  actions: {
    get_comments: {
      "title": "Get comments",
      
      execute: lambda do
        get('https://jsonplaceholder.typicode.com/comments')
      end,
      
      output_fields: lambda do
        [
          {
            name: "data",
            type: "array",
            of: "object",
            properties: [
              { name: "id" },
              { name: "postId" },
              { name: "name" }
            ]
          }
        ]
      end      
    }
  }
}

 

No matter what I try, the error I receive is:


undefined method `to_hash' for #<Array:0x00007f6d738a0808>


What am I doing wrong?


1 person likes this idea

Aalaap Ghag

said over 2 years ago

I figured it out!


You need to parse the response body and add the array to a named property for output fields to access it.

 

...
      execute: lambda do
        get('https://jsonplaceholder.typicode.com/comments').after_response do |code, body, headers|
          {
            comments: body
          }
        end
      end,
...


The list is now showing up. 


3 people like this

Aalaap Ghag

said over 2 years ago

FYI, after the new code block, the output fields block also needs to be updated to match the field name:


 

      output_fields: lambda do
        [
          {
            name: "comments",
            type: "array",
            of: "object",
            properties: [
              { name: "id" },
              { name: "postId" },
              { name: "name" }
            ]
          }
        ]
      end  

 

You can find more info about this and other methods here: SDK - HTTP Methods | Workato Docs


2 people like this

Kevin Hernandez

said over 1 year ago

Thanks for this. 

Login to post a comment

Still can't find your solution?

Send us a ticket, we will try our best to assist you with your problem

Documentation
Developer's Library
Tutorials
eBooks
Product Hour
Product Blog
Workato Blog
Product updates
Customer Stories
© Workato 2022   Privacy   Terms   +1 (844) 469-6752
  • Documentation
  • Workato Blog
  • Product Blog
  • Solutions
  • Forums
  • Tickets
  • Log in
  • Return to Workato
Topic views count