APIs & Integrations

louischausse
Key Advisor | Platinum Partner
Key Advisor | Platinum Partner

How to use the values of a JSON response from an external software as options for a HubSpot property

SOLVE

Hi!

 

I'm creating an integration between an external software and HubSpot

 

What I try to do is (do not hesitate if it is not the best way to do it):

 

First, create an API call to GET the data from external software.

Second, store the values of the JSON response in variables

Finally, use these variables to POST it back to HubSpot

 

The problem is: the JSON response from the API call to the external software can contain an unknown number of objects. (Sometime it could be just 1, sometime it could be 2,3,...,10 or more)

 

So how could I create as many variables as I need to store all this data and be able to reuse it after?

 

I have built a program using node.js

 

I think I have something that makes sense if there would have only one object in the JSON response, but I need help to scale it so it could manage any number of objects.

 

View my code below

 

Here is an example of JSON response from the external software

[
  {
    "id": "my-awesome-program",
    "currency": "USD",
    "title": "My awesome program",
    "cookie_time": 45,
    "default_landing_page_url": "https://my-site.com"
  }
]

But sometime it could have more than one object

[
  {
    "id": "my-awesome-program2",
    "currency": "USD",
    "title": "My awesome program 2",
    "cookie_time": 45,
    "default_landing_page_url": "https://my-site.com"
  }
  {
    "id": "my-awesome-program1",
    "currency": "USD",
    "title": "My awesome program 1",
    "cookie_time": 45,
    "default_landing_page_url": "https://my-site.com"
  }
]

Note that the only values that I'm interested to store are "id" and "title"

 

Here's the node.js module I use:

const express = require('express');
const request = require('request-promise-native');
const NodeCache = require('node-cache');
const session = require('express-session');
const delay = require('delay');

Here's my code

//================================================================//
//   Get a list of programs from the external software            //
//================================================================//

const getPrograms = async () => {  console.log('Retrieving programs');

  const headers = {
    Authorization: `Api-Key: xxxxxx`,
    'Content-Type': 'application/json'
  };

  const programs = { 
  method: 'GET',  url: 'https://api.software1.com/1.6/programs/',  headers: headers,  json: true 
  };  request(programs)
  .then(function (parsedBody) {    console.log(parsedBody);
  })
  .catch(function (err) {     console.log(err);
  });

// Store programs data in reusable variables
// THIS IS WHERE I NEED HELP TO CREATE AS MANY VARIABLES AS OBJECTS IN THE JSON RESPONSE
  const programObj = JSON.parse(parsedBody);
  const programId = programObj.id;
  const programTitle = programObj.title;
};

//====================================================//
//   Post the list of programs to HubSpot             //
//====================================================//

// Creating an "enumeration" type contact property in HubSpot to store programs from the list retrieved from the external software 

const createPrograms = async (accessToken) => {  console.log('Creating a property named programs');

const headers = {
  Authorization: `Bearer ${accessToken}`,
  'Content-Type': 'application/json'
};

const program = {  headers: headers,  method: 'POST',  uri: 'https://api.hubspot.com/properties/v1/contacts/properties',  body: {      name: 'programs',      label: 'Programs',      description: 'programs in which the contact is enrolled',      groupName: 'group_of_properties',      type: 'enumeration',      fieldType: 'select',      options: [ // THIS IS WHERE I NEED HELP TO CREATE AS MANY OPTIONS AS CREATED REUSABLE VARIABLES
        {
          "label": programTitle,
          "value": programId
        },
      ]
  },  json: true // Automatically stringifies the body to JSON
};

Thanks for your help!

Louis Chaussé from Auxilio HubSpot Solutions Partner Signature
Louis Chaussé from Auxilio HubSpot Solutions Partner Meeting link
0 Upvotes
1 Accepted solution
cbarley
Solution
HubSpot Alumni
HubSpot Alumni

How to use the values of a JSON response from an external software as options for a HubSpot property

SOLVE

Hi @louischausse , Cool program you have going here! In order to just get the data you need from what's being returned back to you, you can use some higher order functions such as map:

great youtube video on this: https://www.youtube.com/watch?v=rRgD1yVwIvE

MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

 

For example, if you have this data structure:

 

[
  {
    "id": "my-awesome-program2",
    "currency": "USD",
    "title": "My awesome program 2",
    "cookie_time": 45,
    "default_landing_page_url": "https://my-site.com"
  },
  {
    "id": "my-awesome-program1",
    "currency": "USD",
    "title": "My awesome program 1",
    "cookie_time": 45,
    "default_landing_page_url": "https://my-site.com"
  }
]

 you can use this code to map the objects into something more useful to you. First you can assign the data to a variable:

const myData = [
  {
    "id": "my-awesome-program2",
    "currency": "USD",
    "title": "My awesome program 2",
    "cookie_time": 45,
    "default_landing_page_url": "https://my-site.com"
  },
  {
    "id": "my-awesome-program1",
    "currency": "USD",
    "title": "My awesome program 1",
    "cookie_time": 45,
    "default_landing_page_url": "https://my-site.com"
  }
]

Then we can map over the data and assign it to a new variable or return it from a function:

const newData = myData.map(item => ({
   id: item.id,
   title: item.title
}))

// returns
// [
//   {
//     "id": "my-awesome-program2",
//     "title": "My awesome program 2"
//   },
//   {
//     "id": "my-awesome-program1",
//     "title": "My awesome program 1"
//   }
// ]

After this, if you need each item to then make a call to Hubspot, you can do that with whatever library you want like this:

newData.forEach(item => {
   axios.post(url, config, item).then(res => {
       console.log("...Done!")
   })
});

It'd be best to put this code in a separate function after you've returned all of the data you need in order to ensure that all the data is there. I'd also recommend limiting your calls per second so you don't hit the 10/s rate limit. A good package for that is called bottleneck on NPM. 

Hope this helps!

View solution in original post

1 Reply 1
cbarley
Solution
HubSpot Alumni
HubSpot Alumni

How to use the values of a JSON response from an external software as options for a HubSpot property

SOLVE

Hi @louischausse , Cool program you have going here! In order to just get the data you need from what's being returned back to you, you can use some higher order functions such as map:

great youtube video on this: https://www.youtube.com/watch?v=rRgD1yVwIvE

MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

 

For example, if you have this data structure:

 

[
  {
    "id": "my-awesome-program2",
    "currency": "USD",
    "title": "My awesome program 2",
    "cookie_time": 45,
    "default_landing_page_url": "https://my-site.com"
  },
  {
    "id": "my-awesome-program1",
    "currency": "USD",
    "title": "My awesome program 1",
    "cookie_time": 45,
    "default_landing_page_url": "https://my-site.com"
  }
]

 you can use this code to map the objects into something more useful to you. First you can assign the data to a variable:

const myData = [
  {
    "id": "my-awesome-program2",
    "currency": "USD",
    "title": "My awesome program 2",
    "cookie_time": 45,
    "default_landing_page_url": "https://my-site.com"
  },
  {
    "id": "my-awesome-program1",
    "currency": "USD",
    "title": "My awesome program 1",
    "cookie_time": 45,
    "default_landing_page_url": "https://my-site.com"
  }
]

Then we can map over the data and assign it to a new variable or return it from a function:

const newData = myData.map(item => ({
   id: item.id,
   title: item.title
}))

// returns
// [
//   {
//     "id": "my-awesome-program2",
//     "title": "My awesome program 2"
//   },
//   {
//     "id": "my-awesome-program1",
//     "title": "My awesome program 1"
//   }
// ]

After this, if you need each item to then make a call to Hubspot, you can do that with whatever library you want like this:

newData.forEach(item => {
   axios.post(url, config, item).then(res => {
       console.log("...Done!")
   })
});

It'd be best to put this code in a separate function after you've returned all of the data you need in order to ensure that all the data is there. I'd also recommend limiting your calls per second so you don't hit the 10/s rate limit. A good package for that is called bottleneck on NPM. 

Hope this helps!