Best answer

Passing complex nested JSON via custom request

  • 4 January 2022
  • 5 replies
  • 63 views

Hello! I am working on a Zap that will require me to make a quite complex API POST using nested JSON (the sample I’ve been working with in Postman is ~425 lines of code). Within that JSON request, I need to populate specific fields from a previous step in my Zap (some custom Python code that’s outputting a nested dictionary). I’m comfortable formatting the Python output to isolate the variables on their own, but I’m having trouble navigating the complex JSON code to place the variables where they belong, because of the small edit window and the lack of the ability to add spacing/tabs.

Would it be easier to somehow create a template of text with MOST of the JSON code, and then somehow inject the variables within it? Is this something that really should be a custom built integration via the Developer Tools? Is there an easier way to map values in a heavily nested JSON request?

Thanks in advance!

icon

Best answer by a.mitchell 10 January 2022, 18:43

View original

5 replies

Userlevel 7
Badge +10

Welcome to the Community, @a.mitchell! :)

When using the Custom Request action event (Webhooks by Zapier) it should be possible to just copy and paste in your templated JSON code into the Data field and then replace the relevant variables with values taken from previous steps. For example:

d11135279cc066d73aa1e8af18ebc6e8.png

Alternatively, instead of selecting each variable individually in the Webhook step, could the Python code be updated to output the variables in JSON format? That way you could potentially set up the Webhook step to use the JSON that’s output from the Code step, to pass it multiple variables without having to select each individual field. 

Do you think that approach could work?

Thanks for the response, @SamB!

Using the “paste and replace” method was what I had been trying, but pasting my templated code in from Postman does not preserve any of the formatting, so I see something like this:

I’d love to avoid having to go through and re-format 400+ lines of code just to replace the variables ;)

As for Python into JSON, my understanding is there’s a great JSON library that does just this, but that the Code by Zapier task doesn’t support loading external libraries. Is that correct? Might the dev team be open to allowing JSON to be loaded, given how often it’s used in other tasks within Zapier?

Thanks!

Userlevel 7
Badge +10

Thanks for getting back to me @a.mitchell

Hmm, this is very strange indeed! I just tried copying the following code over from Postman:
ba8e4f332d29a28ecb17d544bc9e9464.png
and it appears to keep the formatting in the Webhook step:
d110d10d1bc831b8ec8e2395e3e3eb00.png

If you paste the code from Postman directly into a text editor of some kind does the formatting remain or is it also removed? If the formatting remains in the text editor, can you then try re-copying it from the text editor and pasting it into the Webhook step? I’m wondering if that might allow the formatting to be copied across correctly.

Alternatively, I wonder if this could be some sort of glitch caused by the amount of code being pasted in. If you copy and paste in a smaller amount of code does the formatting remain?

You’re correct about the external libraries, they aren’t currently supported. We do have a feature request open for this so I’ve added your vote for that. Unfortunately I don’t have any estimate as to when they will be supported but we’ll be sure to email you as soon as they are!

Hi, @SamB - thanks for following up! Looks like my browser choice was the culprit - pasting from Postman into a Custom Request action using Safari did not preserve formatting, but the minute I switched to Chrome, all was solved! I’ll be able to use the custom request field and can much more easily map the necessary fields now that the request is properly formatted. Thanks so much for your help :)

Userlevel 5
Badge +7

Ah, thanks so much for updating us all with the solution, @a.mitchell! 🎉

Reply