How to Rebuild RAW JSON for Code Blocks

  • 17 February 2022
  • 7 replies
  • 595 views

Userlevel 7
Badge +12

THIS IS CONSIDERED ADVANCED and requires Javascript knowledge but solves the issue of rebuilding raw json and line items to use in code without compromising the integrity like a catch raw hook trigger might. 

Let’s say we have line items returned from a trigger and want to rebuild the original JSON structure to parse in a code block. 

As a sample, our line items look like the below which results in Zapier merging everything into like fields which can cause issues with null values, nested arrays, text that has commas.


 



The solution!  
 

You can rebuild your raw json text structure using a Formatter step > Utilities > Line Items to Text. 
Take special consideration of preserving the field types. strings should be surrounded by double quotes “”, if a field was a line item you should surround it with square brackets [], etc… 


You should get back something that resembles raw json which can then be mapped into a code step surrounded by square brackets allowing you to use things like json.parse to iterate through your data preserving your indexes.


7 replies

Userlevel 7
Badge +11

Awesome tip, @GetUWired! Thanks for sharing :)

Not working on my side !?

First step not working, here’s details with images….

Blue (array of 2 items) should match the blue and red match the red each color associated with it’s right color….. 1. Line Items Meta Data Display value isn’t kept separated… it is merged as like it was the same array red + blue from the first image...

 

Here’s the inputs

 

Here’s the output !!!

 

What’s wrong, the initial array are not kept ?

Userlevel 7
Badge +12

Hi @GandalfG 

Can you share screenshots of the json as it appears from your trigger step? 

My first guess would be that iListAtts and iListsAttsSelects are not nested arrays like you’ve built your json but rather an object with two arrays in it. Thus, i would remove the extra square brackets surrounding them. 

 

 

Userlevel 1

@GetUWired Thanks for sharing – however I can’t seem to recreate your setup.

Particularly, I am having trouble recreating the setup required to return “[10, 15], null”.

When I have nested line items, the nulls are ignored entirely.

e.g. I create a sample return in a Code by Zapier box:

output = {result: [
{id: 123, hello: "world", sub: [{id:1, name:"bob"}] },
{id: 124, hello: "world2", sub: [{id:2, name:"bob2"},{id:3, name:"bob3"}] },
{id: 125, hello: "world3", sub: null},
]};

In subsequent steps, the input choices look like this:

 

So, how did you manage to get the array [10,15],null to appear? If I can’t even get it when feeding it the JSON directly via a JS code block, then what else can I try? Without the array brackets showing up, the formatter can’t decipher what should be in an array/object as opposed to just a comma-separated list.

In the end, I am looking to be able to process sub- Line Items, i.e. provide a list of items, each one of which has one or more fields that are arrays of foreign key ids.

 

thanks

Stephen

Userlevel 7
Badge +12

@StephenB 

Unfortunately, we have come to find out this solution does not work with complex nested arrays. 
The structure of your data is an array with many objects that all have their own arrays with nested objects. 

Our solution ideally works best on line items with only a nested object structure. and works okay for simple text or number arrays in those object but does not work well for arrays that have nested arrays with their own object structures. 

ie…

[

{
"id": 1,
"name": "test1"
},

{
"id": 2,
"name": "test2"
}

]



it will rebuild no problem. and we’ve come to find it should handle situations like this

[

{
"id": 1,
"name": "shirt",
"qtys": [1,2],
"sizes": ["Large","x-large"]
},

{
"id": 2,
"name": "some other item",
"qtys": [1],
}

]





but it cannot handle these types of situations in which you are trying to rebuild an array with objects nested within another array:

[
{id: 123, hello: "world", sub: [{id:1, name:"bob"}] },
{id: 124, hello: "world2", sub: [{id:2, name:"bob2"},{id:3, name:"bob3"}] },
{id: 125, hello: "world3", sub: null},
]



 


If you need to rebuild nested arrays like these we suggest using a POST Webhook action with passthrough enabled (immediately after the step you are trying to rebuild) to a second Zap that triggers by the Catch RAW Webhook trigger. You can then use a code block (in the second zap) to manipulate the raw json results however you’d like.

Userlevel 1

Thanks @GetUWired that’s really helpful. There seems to be all kinds of idiosyncrasies with this…

Cheers
Stephen

 

@GetUWired Is my data structure shown below one that could only be handled by the raw webhook as you described?

 

Reply