Skip to main content
Best answer

Zapier CLI app how to submit nested JSON?


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 :)

Best answer by ikbelkirasanBest answer by ikbelkirasan

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

 

View original
Did this topic help you find an answer to your question?
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

ikbelkirasan
Forum|alt.badge.img+12
  • Zapier Expert
  • 555 replies
  • Answer
  • April 14, 2020

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

 


  • Author
  • Beginner
  • 1 reply
  • April 14, 2020

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.


ikbelkirasan
Forum|alt.badge.img+12
  • Zapier Expert
  • 555 replies
  • April 14, 2020

@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.