重新映射 json 对象以进行消息传递
re-map json object for messaging
我正在尝试将 Twitter 直接消息数据重新映射到 mongodb 的对话中。
"events": [
{
"type": "message_create",
"id": "1023372540847312900",
"created_timestamp": "1532826001737",
"message_create": {
"target": {
"recipient_id": "605675237"
},
"sender_id": "1020464064684806146",
"source_app_id": "268278",
"message_data": {
"text": "all right mate thank you too",
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
}
}
}
},
{
"type": "message_create",
"id": "1023372444210524164",
"created_timestamp": "1532825978697",
"message_create": {
"target": {
"recipient_id": "1020464064684806146"
},
"sender_id": "605675237",
"message_data": {
"text": "Okay thank you mate, this is distinquish",
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
}
}
}
},
{
"type": "message_create",
"id": "1023372325150965765",
"created_timestamp": "1532825950311",
"message_create": {
"target": {
"recipient_id": "1020464064684806146"
},
"sender_id": "69565292",
"message_data": {
"text": "Hello strow bree how are you",
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
}
}
}
},
{
"type": "message_create",
"id": "1023245595790778372",
"created_timestamp": "1532795735677",
"message_create": {
"target": {
"recipient_id": "605675237"
},
"sender_id": "1020464064684806146",
"source_app_id": "268278",
"message_data": {
"text": "Once at a social gathering, Gladstone said to Disraeli",
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
}
}
}
},
{
"type": "message_create",
"id": "1023245133637140484",
"created_timestamp": "1532795625491",
"message_create": {
"target": {
"recipient_id": "1020464064684806146"
},
"sender_id": "605675237",
"message_data": {
"text": "On Krat's main screen appeared the holo image of a man, and several dolphins. From the man's shape, Krat could tell it was a female, probably their leader. \"...stupid creatures unworthy of the name `sophonts.' Foolish, pre-sentient upspring of errant masters. We slip away from all your armed might, laughing at your clumsiness! We slip away as we always will, you pathetic creatures. And now that we have a real head start",
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
}
}
}
}
],
"apps": {
"268278": {
"id": "268278",
"name": "Twitter Web Client",
"url": "http://twitter.com"
}
}
}
我正在尝试将此数据更改为此数据;
[{
members: [ '605675237', '1020464064684806146' ],
messages: [
{ author: '605675237', body: 'On Krats main screen appeared the holo image of a man, and several dolphins. From the mans shape, Krat could tell it was a female, probably their leader ...stupid creatures unworthy of the name sophonts Foolish, pre-sentient upspring of errant masters. We slip away from all your armed might, laughing at your clumsiness! We slip away as we always will, you pathetic creatures. And now that we have a real head start' },
{ author: '1020464064684806146', body: 'Once at a social gathering, Gladstone said to Disraeli' }
{ author: '605675237', body: 'Okay thank you mate, this is distinquish' }
{ author: '1020464064684806146', body: 'all right mate thank you too' },
]
},
{
members: ['69565292', '1020464064684806146'],
messages: [
{ author: '69565292', body: 'Hello strow bree how are you' }
]
}]
user_id1 必须是 sender_id 并且 user_id2 必须是 recipient_id 但实际上,我必须按 sender_id 和 [= 对 twitter DM 对象进行分组26=]可能。
有什么建议可以轻松解决这个问题吗?
也许我们可以使用 Lodash 或 Underscore 轻松解决此重映射问题。
解决方案是按用户 ID 对消息进行分组,我从 ID 创建了一个散列以将其放入一个对象中:
const normalized = {};
data.events.forEach(event => {
// Just of easier access
const message = event.message_create;
// Create a temp hash which identify 2 user conversation.
// The hash is just the 2 ids with a `|` separated them
const hashedUsers = [message.sender_id, message.target.recipient_id].sort().join('|');
// The end object you wanted
const normalizedMessage = {
author: message.sender_id,
body: message.message_data.text
};
// Check if we already have this 2 users conversations, create a list of their messages
if (!normalized.hasOwnProperty(hashedUsers)) {
normalized[hashedUsers] = [normalizedMessage];
} else {
normalized[hashedUsers].push(normalizedMessage);
}
});
// Now we have a normalized object, the keys are the hash we created and the values are their messages
// so we can create the object you wanted:
const output = []
Object.entries(normalized).forEach(([key, value]) => {
output.push({
members: key.split('|'),
messages: value
})
})
我正在尝试将 Twitter 直接消息数据重新映射到 mongodb 的对话中。
"events": [
{
"type": "message_create",
"id": "1023372540847312900",
"created_timestamp": "1532826001737",
"message_create": {
"target": {
"recipient_id": "605675237"
},
"sender_id": "1020464064684806146",
"source_app_id": "268278",
"message_data": {
"text": "all right mate thank you too",
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
}
}
}
},
{
"type": "message_create",
"id": "1023372444210524164",
"created_timestamp": "1532825978697",
"message_create": {
"target": {
"recipient_id": "1020464064684806146"
},
"sender_id": "605675237",
"message_data": {
"text": "Okay thank you mate, this is distinquish",
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
}
}
}
},
{
"type": "message_create",
"id": "1023372325150965765",
"created_timestamp": "1532825950311",
"message_create": {
"target": {
"recipient_id": "1020464064684806146"
},
"sender_id": "69565292",
"message_data": {
"text": "Hello strow bree how are you",
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
}
}
}
},
{
"type": "message_create",
"id": "1023245595790778372",
"created_timestamp": "1532795735677",
"message_create": {
"target": {
"recipient_id": "605675237"
},
"sender_id": "1020464064684806146",
"source_app_id": "268278",
"message_data": {
"text": "Once at a social gathering, Gladstone said to Disraeli",
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
}
}
}
},
{
"type": "message_create",
"id": "1023245133637140484",
"created_timestamp": "1532795625491",
"message_create": {
"target": {
"recipient_id": "1020464064684806146"
},
"sender_id": "605675237",
"message_data": {
"text": "On Krat's main screen appeared the holo image of a man, and several dolphins. From the man's shape, Krat could tell it was a female, probably their leader. \"...stupid creatures unworthy of the name `sophonts.' Foolish, pre-sentient upspring of errant masters. We slip away from all your armed might, laughing at your clumsiness! We slip away as we always will, you pathetic creatures. And now that we have a real head start",
"entities": {
"hashtags": [],
"symbols": [],
"user_mentions": [],
"urls": []
}
}
}
}
],
"apps": {
"268278": {
"id": "268278",
"name": "Twitter Web Client",
"url": "http://twitter.com"
}
}
}
我正在尝试将此数据更改为此数据;
[{
members: [ '605675237', '1020464064684806146' ],
messages: [
{ author: '605675237', body: 'On Krats main screen appeared the holo image of a man, and several dolphins. From the mans shape, Krat could tell it was a female, probably their leader ...stupid creatures unworthy of the name sophonts Foolish, pre-sentient upspring of errant masters. We slip away from all your armed might, laughing at your clumsiness! We slip away as we always will, you pathetic creatures. And now that we have a real head start' },
{ author: '1020464064684806146', body: 'Once at a social gathering, Gladstone said to Disraeli' }
{ author: '605675237', body: 'Okay thank you mate, this is distinquish' }
{ author: '1020464064684806146', body: 'all right mate thank you too' },
]
},
{
members: ['69565292', '1020464064684806146'],
messages: [
{ author: '69565292', body: 'Hello strow bree how are you' }
]
}]
user_id1 必须是 sender_id 并且 user_id2 必须是 recipient_id 但实际上,我必须按 sender_id 和 [= 对 twitter DM 对象进行分组26=]可能。
有什么建议可以轻松解决这个问题吗?
也许我们可以使用 Lodash 或 Underscore 轻松解决此重映射问题。
解决方案是按用户 ID 对消息进行分组,我从 ID 创建了一个散列以将其放入一个对象中:
const normalized = {};
data.events.forEach(event => {
// Just of easier access
const message = event.message_create;
// Create a temp hash which identify 2 user conversation.
// The hash is just the 2 ids with a `|` separated them
const hashedUsers = [message.sender_id, message.target.recipient_id].sort().join('|');
// The end object you wanted
const normalizedMessage = {
author: message.sender_id,
body: message.message_data.text
};
// Check if we already have this 2 users conversations, create a list of their messages
if (!normalized.hasOwnProperty(hashedUsers)) {
normalized[hashedUsers] = [normalizedMessage];
} else {
normalized[hashedUsers].push(normalizedMessage);
}
});
// Now we have a normalized object, the keys are the hash we created and the values are their messages
// so we can create the object you wanted:
const output = []
Object.entries(normalized).forEach(([key, value]) => {
output.push({
members: key.split('|'),
messages: value
})
})