Skip to main content
Best answer

How to a add an id to returned JSON in Zapier Platform

  • August 26, 2021
  • 1 reply
  • 171 views

I am building in the Zapier platform doing a get request out to our servers and it is returned the following JSON 

- Results must be an array, got: object, ({"1131810":{"campaigns":[{"campaignId":35924,"camp)
  - Got a result missing the "id" property ({"campaigns":[{"campaignId":35924,"campaignName":"HR Job: Test - Electrical Engineer - 2021-08-19 test 1"}],"fields":{"email":"email@gmail.com","mobile":1412,"name":"Nick  Tester"}})

 

How do a change the return to make the first value (e.g. 1131810 be the “id”) and then get all the other responses. 

 

 const timestart =Math.floor(Date.now() / 1000) -  bundle.inputData.validthrutimestamp;
    const endtime = Math.floor(Date.now() / 1000) - bundle.inputData.endtime


const options = {
  url: `${bundle.authData.server}/org/${bundle.authData.orgId}/contacts`,
  method: 'GET',
  headers: {
    'Accept': 'application/json',
    'Authorization': `Token ${bundle.authData.token}`
  },
  params: {
    'reportType': bundle.inputData.statustype,
    'startTimestamp': timestart,
    'endTimestamp': endtime,
    'list': `[${bundle.inputData.fields}]`
  }
}

return z.request(options)
  .then((response) => {
       response.throwForStatus();
     if (response.status == 401 || response.status == 400 )  {
    throw new z.errors.RefreshAuthError();
  }
    const results = response.json;

    // You can do any parsing you need for results here before returning them

    return results;
  });

 

Best answer by ZaneBest answer by Zane

I think you’ll need some javascript to change the shape of that object prior to returning it. Something like:

 

const example = {

    "1131810":{

        "campaigns":[

            {"campaignId":35924,"campaignName":"blue"},

            {"campaignId":2222,"campaignName":"red"}]

    },

    "2222222":{

        "campaigns":[

            {"campaignId":11111,"campaignName":"black"},

            {"campaignId":33333,"campaignName":"white"}]

    }

}

const results = []

for (const [key, value] of Object.entries(example)) {

  results.push({id: key, value: value})

}

console.log(JSON.stringify(results))

 

/** will print:

[{

  "id": "1131810",

  "value": {

    "campaigns": [{

      "campaignId": 35924,

      "campaignName": "blue"

    }, {

      "campaignId": 2222,

      "campaignName": "red"

    }]

  }

}, {

  "id": "2222222",

  "value": {

    "campaigns": [{

      "campaignId": 11111,

      "campaignName": "black"

    }, {

      "campaignId": 33333,

      "campaignName": "white"

    }]

  }

}]

    

**/

 

That’s assuming that’s the entity you want to return. You might want, say, the campaign to be the entity that trigger returns, in which case your transform will be different.

View original
Did this topic help you find an answer to your question?
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

1 reply

Forum|alt.badge.img+9
  • Zapier Staff
  • 331 replies
  • Answer
  • August 31, 2021

I think you’ll need some javascript to change the shape of that object prior to returning it. Something like:

 

const example = {

    "1131810":{

        "campaigns":[

            {"campaignId":35924,"campaignName":"blue"},

            {"campaignId":2222,"campaignName":"red"}]

    },

    "2222222":{

        "campaigns":[

            {"campaignId":11111,"campaignName":"black"},

            {"campaignId":33333,"campaignName":"white"}]

    }

}

const results = []

for (const [key, value] of Object.entries(example)) {

  results.push({id: key, value: value})

}

console.log(JSON.stringify(results))

 

/** will print:

[{

  "id": "1131810",

  "value": {

    "campaigns": [{

      "campaignId": 35924,

      "campaignName": "blue"

    }, {

      "campaignId": 2222,

      "campaignName": "red"

    }]

  }

}, {

  "id": "2222222",

  "value": {

    "campaigns": [{

      "campaignId": 11111,

      "campaignName": "black"

    }, {

      "campaignId": 33333,

      "campaignName": "white"

    }]

  }

}]

    

**/

 

That’s assuming that’s the entity you want to return. You might want, say, the campaign to be the entity that trigger returns, in which case your transform will be different.