Open Navigation

JSON Reader

JSON Reader paginates very large JSON files in order to make them easier to work with when using the Tray.io platform.

Overview

The main purpose of the JSON Reader core connector is to allow for the pagination of very large JSON files.

Operations List

  • Paginate JSON

Example usage

In this example we imagine a scenario where you need to add thousands of contacts that are in a JSON file, directly into your Salesforce account. The file that we are processing will look like this (with the contacts array containing the data we want to use):

{
"Contacts": [
{
"_id": "5e14b3800ed46b4e1df63030",
"name": "Ratliff Welch",
"gender": "male",
"company": "EYERIS",
"email": "ratliffwelch@eyeris.com",
"phone": "+1 (880) 443-2935",
"address": "651 McKibben Street, Iberia, New Jersey, 4539"
},
{
"_id": "5e14b38028f2eedb175b0058",
"name": "Fowler Humphrey",
...........
}

The steps will be as follows:

  1. Set up your trigger and download a large JSON file.
  2. Loop through said JSON file.
  3. Create a break condition for the loop connector.
  4. Either add the necessary contacts into a Salesforce account, or halt workflow.

The complete workflow will look something like this:

json-reader-complete-flow

1 - Setup and download the JSON file

First go to your Tray.io dashboard, click on the 'Create new workflow' button in the top right corner, and name your project workflow. Select 'Manual' as your trigger. Your operation should automatically be set to 'trigger'.

Once redirected to the workflow dashboard, select the 'File Helpers' helper connector from the connector panel (on the left hand side). Place it on the next step of your workflow.

We have renamed this step to reflect its intended purpose and would suggest that you do the same so that you can keep better track of what your steps are doing at each stage.

Make sure your operation is set to 'Create File from URL'. The data we are testing with can be seen at the following URL. Make sure to copy the URL from the address bar accurately else the workflow will fail.

The data and URL can be found here and/ or here: https://tray.io/documentation/platform/connectors/docs/helpers/json-reader/test.json.

All your other inputs are either not mandatory or filled in for you.

json-reader-step-1

Once we input the URL of where our file is hosted, it will return a file object that we can use in our JSON Reader workflow.

2 - Loop through the JSON file

Next you will need to add the 'Loop collection' core connector to the next step of your workflow, with its operation set to 'Loop forever'. This is to enable your workflow to "loop continuously" through the items in your JSON file.

Then add the 'JSON Reader' core connector to your loop step. Make sure the operation is set to 'Paginate JSON'.

Use the connector-snake to auto-generate your JSON path for the 'File' input field as displayed below:


Your 'Is array?' option will automatically be ticked, so move to the next step and set your 'Path' to Contacts. Your 'Page size' will have 100 displayed which we have changed this to 10 for the sake of our demo. Finally generate your 'Page number' JSON path the same way you did for your 'File' field

json-reader-connector

3 - Create a break clause

We need a way to stop our workflow from looping forever once the task is complete, so the next step will be to add a 'Boolean condition' core connector so that our workflow can check if our results (or rather $.steps.json-reader-1.results), is equal to zero/ an empty array: []. When this condition returns true we will then have looped through the entire JSON array and essentially finished our JSON pagination.

Make sure the operation is set to 'Boolean condition' and that your "input selector" is set to 'jsonpath' for your '1st Value' (see highlighted section below for clarification). Use the connector-snake to get your previous step's results to fill in the field value.

'Comparision type' should be 'Equal to', and the '2nd Value' has its "input selector" is set to 'string' with an empty array displayed: [].

json-reader-boolean-1

4 - Add contacts or end workflow

Add the Salesforce service connector to the FALSE branch of your loop. Set the operation to 'Create Record' with the record type set to 'Contact'.

If you do not have a Salesforce account, please see our Salesforce page for details.

Click on the greyed out 'Add Field' button below to generate the number of fields required.

json-reader-salesforce-1

You should get a new section created that looks like this:

json-reader-salesforce-2

Keep adding as many fields as required. For our demo we have simply used name and email address from the field options available in our test.json file, but feel free to use more should you so wish.

Note that the JSON path has once more been generated through the connector-snake and should be similar to this: $.steps.json-reader-1.results[0].name depending on your chosen fields.

json-reader-salesforce-1

Then add a 'Break Loop' core connector to the TRUE branch of the boolean condition, making sure that the operation is set to 'Break' and that 'loop-1' is what is selected in the 'Break' field itself:

json-reader-break-loop

Setup should now be complete!

Was this article helpful?
Yes
No