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-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"
//================================================================//// 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 RESPONSEconst 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};
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:
[{"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:
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.
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:
[{"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:
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.