我正在创建两个软件之间的集成,我需要从software1获取数据并将其发布到software2。
我想做的是(如果不是最好的方法,请不要犹豫):
首先,创建一个API调用以从software1中获取数据。 其次,将JSON响应的值存储在变量中 最后,使用这些变量将其回发到software2
问题是:从API调用到软件1的JSON响应可能包含未知数量的对象。 (有时可能只是,有时可能是2,3,...,10或更多)
那么我该如何创建所需数量的变量来存储所有这些数据并在以后重用呢?
我已经使用node.js构建了一个程序
如果在JSON响应中只有一个对象,我想我很有道理,但是我需要帮助扩展它,以便它可以管理任意数量的对象。
在下面查看我的代码
以下是来自该软件的JSON响应示例
[
{
"id": "my-awesome-program",
"currency": "USD",
"title": "My awesome program",
"cookie_time": 45,
"default_landing_page_url": "https://my-site.com"
}
]
但是有时它可能有多个对象
[
{
"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"
}
]
请注意,我要存储的唯一值是“ id”和“ title”
这是我使用的nodejs模块:
const express = require('express');
const request = require('request-promise-native');
const NodeCache = require('node-cache');
const session = require('express-session');
const delay = require('delay');
这是我的代码
//====================================================//
// Get a list of programs from software1 //
//====================================================//
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 in software2 //
//====================================================//
// Creating a contact property in software2 to store programs from the list retrieved from software1
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.software2.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
};
感谢您的帮助!
答案 0 :(得分:0)
我在另一个论坛上得到了答案,所以我想可以在这里粘贴它,以帮助社区。 p>
为了只从返回的数据中获取所需的数据,可以使用一些高阶函数,例如map:
关于此的伟大的youtube视频:https://www.youtube.com/watch?v=rRgD1yVwIvE
MDN:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/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"
}
]
您可以使用此代码将对象映射为对您更有用的内容。首先,您可以将数据分配给变量:
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"
}
]
然后,我们可以映射数据并将其分配给新变量或从函数返回它:
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"
// }
// ]
此后,如果您需要每个项目然后调用Hubspot,则可以使用所需的任何库来做到这一点:
axios.post(url, config, item).then(res => {
console.log("...Done!")
})
});
最好在返回所有需要的数据后,将此代码放在单独的函数中,以确保所有数据都在其中。我还建议您限制每秒的调用次数,以免达到API的X / s速率限制。一个好的解决方案称为NPM瓶颈。