Best answer

Parse nested JSON object?


Userlevel 1

How can we access nested JSON object elements in a Zapier Webhook?

I tried with javascript and json.parser and that’s not work for me

 

icon

Best answer by SamB 8 September 2021, 17:08

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.

12 replies

Userlevel 7
Badge +10

Hi @minuf I’ve used nested elements from a JSON object inside Zapier many times, but I’m not sure what your data looks like and where you are getting stuck. Some screenshots might help, basically what is the output of the “send data” portion of your step, and what options are you able to select from for the action step that you want the values for?

Userlevel 1

Here is an example of my data.

I need some of the fields that inside “meta_data” Like “TransactionId” and CardHolderID.

Userlevel 7
Badge +10

@minuf this is not from a webhook or via a Zapier platform application you control is it?

 

If it is, then you can do processing inside the Zapier platform before it gets provided to the zap.

If not then what did your javascript/JSON.parse look like… I’m imagining a code step right?

 

I have been successful at using formatter steps to pull out specific values but it takes multiple steps and is inefficient at scale (split text on “TransactionId” use the second result, then split on “,” and get the first result, now you have your value of Transaction ID (in quotes)) but I’ve also done similar in javascript which is significantly more efficient. 

 

Knowing if you are processing this inside the platform or in a zap will better inform the code suggestions.

Userlevel 7
Badge +11

Hi @minuf!

Since this was a little while ago, I wanted to check in to see if you’d managed to solve this or whether you still needed help here. Please let us know!

Userlevel 1

I have this same problem with a webhook.  The root data appears to be translated, but nested data seems to be in a blob field.  Is this a problem with the source json block of data or with zapier?  Answering some of the other questions above - I don’t control the source of the hook, but I can influence it, if that is the problem.  Thanks for the help! -Adam

Userlevel 7
Badge +10

@AdamG Is the blob valid JSON?

 

If so, you could run a code step like so:

 

And the output would be all of the obects in the JSON as values like so:

 

Code for copying:

let smooth = JSON.parse(inputData.nestedJSON);
output = [{value: smooth}];

 

Userlevel 1

Interesting solution.  It isn’t an actual blob.  My Output from the hook is very similiar to the Output by the previous user.  The top level fields seem to be definined; however sub record data is just key value pairs.  I am trying to understand if this is just how zapier works or if the inbound JSON is not formatted properly.  

I did try your soultion, but there is no single field out of the webhook object that is the entire dataset to input into the JSON parser, that I can find.

Example:  stuff before and after:

 

 

Userlevel 7
Badge +10

@AdamG the “History” is JSON but it’s not valid JSON, meaning there should be a comma between each key value pair…

here’s a snippet of that text

employee:{(stuff here)}
eventType:Job Booked
id: 400049721776

If it were proper JSON it would be formatted like this:

{
employee:{(stuffhere)},
eventType:'Job Booked',
id: 400049721776
}

So yeah you need to add commas and quotes as needed plus opening and closing curly braces { }

But if the original text had that Zapier would have parsed the JSON correctly.

Userlevel 7
Badge +10

This is a helpful tool I use to validate JSON, but it can be a little overzealous (has me quote all keys) https://jsonlint.com/

Userlevel 1

Awesome!  Thanks for the help, explaining the json is invalid.  I will go back to the originator of the webhook and see if they can fix it.  -Adam

Userlevel 1

Hi there

I have a .json file with nested data being uploaded into an S3 bucket. This upload is the trigger for a zap and a number of posts to various endpoints. Is it possible to use JSON.parse or a variation to extract the data within the file for use later on in the Zap?

If not possible using a code step, is there another way I should approach this? 

Many thanks

Joe

 

S3 trigger from .json file upload

 

extract from data within .json file

 

Userlevel 7
Badge +11

Just wanted to jump in to summarize an answer here!

If the JSON coming from a webhook is valid, it should be possible to use JSON.parse in a Code by Zapier step to parse out the relevant fields as per PaulKortman’s post above.

If it’s not valid, you can try using Formatter (Text) actions to extract and split information out into separate fields but this would likely involve numerous additional steps and is less efficient than using a single code step.

To confirm whether the JSON is valid you can use a tool like JSONLint to validate it.

And for issues with parsing key value pairs specifically, TimS wrote an article in Community which explains how to do that: