Search code examples
zapierzapier-cli

Zapier - add data to JSON response (App development)


We are creating a Zapier app to expose our APIs to the public, so anyone can use it. The main endpoint that people are using returns a very large and complex JSON object. Zapier, it looks like, has a really difficult time parsing nested complex JSON. But it does wonderful with a very simple response object such as

{ "field": "value" }

Our data that is being returned has this structure and we want to move some of the fields to the root of the response so it's easily parsed by Zapier.

"networkSections": [
    {
        "identifier": "Deductible",
        "label": "Deductible",
        "inNetworkParameters": [
            {
                "key": "Annual",
                "value": " 600.00",
                "message": null,
                "otherInfo": null
            },
            {
                "key": "Remaining",
                "value": " 600.00",
                "message": null,
                "otherInfo": null
            }
        ],
        "outNetworkParameters": null
    },

So, can we do something to return for example the remaining deductible?

I got this far (adding outputFields) but this returns an array of values. I'm not sure how to parse through this array either in the Zap or in the App.

 {key: 'networkSections[]inNetworkParameters[]key', label: 'xNetworkSectionsKey',type: 'string'},

ie this returns an array of "Annual", "Remaining", etc


Solution

  • Great question. In this case, there's a lot going on, and outputFields can't quite handle it all. :(

    In your example, inNetworkParameters contains an array of objects. Throughout our documentation, we refer to these as line items. These lines items can be passed to other actions, but the different expected structures presents a bit of a problem. The way we've handled this is by letting users map line-items from one step's output to another step's input per field. So if step 1 returns

    {
      "some_array": [
        {
          "some_key": "some_value"
        }
      ]
    }
    

    and the next step needs to send

    {
      "data": [
        {
          "some_other_key": "some_value"
        }
      ]
    }
    

    users can accomplish that by mapping some_array.some_key to data.some_other_key.

    All of that being said, if you want to always return a Remaining Deductible object, you'll have to do it by modifying the result object itself. As long as this data is always in that same order, you can do something akin to

    var data = z.JSON.parse(bundle.response.content);
    data["Remaining Deductible"] = data.networkSections[0].inNetworkParameters[1].value;
    return data;
    

    If the order differs, you'll have to implement some sort of search to find the objects you'd like to return.

    I hope that all helps!