Best answer

Zapier CLI app how to submit nested JSON?

  • 14 April 2020
  • 3 replies
  • 2701 views

Userlevel 1

Hi, guys!

I have converted my app to Zapier CLI platform and trying to add new action using JS.

But I have an issue with submitting the inputData to the server.

The thing is that I’m using nested JSON structure that backed supports:

{
"custom_object": {
"resource_id": 123,
"resource_type": "Contact"
}
}

Hence, my keys in inputFields looks like “custom_object__resource_id”, “custom_object__resource_type”.

In the legacy actions, perform method looks like below and it turns __ into nested object:

const perform = (z, bundle) => {
return z.legacyScripting.run(bundle, 'create', 'update_section');
};

But how should I write the new “perform” function to convert my keys “custom_object__resource_id” into  the structure I need? Is there a helper method I can apply or something?

When I simply do this:

const perform = (z, bundle) => {
const promise = z.request({
url: 'https://myapp.com/api/v1/sections',
method: 'POST',
body: bundle.inputData,
headers: {
'content-type': 'application/json'
}
});

return promise.then(response => JSON.parse(response.content));
};

it just sends the inputData as is (“custom_object__resource_id”) which is not acceptable by the backend.

Any help would be appreciated :)

icon

Best answer by ikbelkirasan 14 April 2020, 13:18

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

3 replies

Userlevel 7
Badge +12

Hi @mojobiri - Try using the flat package from npm. Here’s how to use it to unflatten the entire inputData object.

const { unflatten } = require("flat");

// ...

const data = unflatten(bundle.inputData, {
delimiter: "__",
object: true
});

 

Userlevel 1

Thank you @ikbelkirasan Will try it out. I thought there is some built in functionality, but if there is no support for that, your solution is pretty straightforward and easy replacement.

Userlevel 7
Badge +12

@mojobiri - The legacy runner seems to have a function to unflatten the object, which doesn’t exist in the zapier-platform-core. So, you have two options, you can either copy the unflattenObject function from the legacy runner into your code, or use the flat package I mentioned earlier which does pretty much the same thing.