Node.js处理Twitter API返回JSON:如何将media_keys替换为预览URL
实现Twitter API返回结果的JSON结构转换
需求背景
我正在使用Node.js开发对接Twitter API的应用,Twitter API返回的JSON结构示例如下:
result = { "data": [ { "id": "1410481286947483648", "created_at": "2021-07-01T06:11:58.000Z", "text": "RT @weatherdak: Absolutely mind-blowing wildfire behavior in British Columbia.\n\nIncredible & massive storm-producing pyrocumulonimbus plume…" }, { "attachments": { "media_keys": [ "7_1408149248726667266" ] }, "id": "1408168068237434883", "created_at": "2021-06-24T21:00:04.000Z", "text": "RT @Brophyst: Tornado in Brno, Czech Republic :O " }, { "attachments": { "media_keys": [ "16_1406870837026770949" ] }, "id": "1406870843972632579", "created_at": "2021-06-21T07:05:22.000Z", "text": "Working on a new storm chasing application with incoming thunder rumbling. " }, { "id": "1405081317985947648", "created_at": "2021-06-16T08:34:26.000Z", "text": "@thefunkygirl @TornadoGreg @cgphotography Slender tornadoes are (almost) the best!" } ], "includes": { "media": [ { "media_key": "7_1408149248726667266", "preview_image_url": "https://pbs.twimg.com/ext_tw_video_thumb/1408149248726667266/pu/img/qNKbx0MtaSGG0Sv0.jpg", "type": "video" }, { "media_key": "16_1406870837026770949", "preview_image_url": "https://pbs.twimg.com/tweet_video_thumb/E4Y1koXWUAU1Ls5.jpg", "type": "animated_gif" } ] }, "meta": { "oldest_id": "1405081317985947648", "newest_id": "1410481286947483648", "result_count": 10, "next_token": "7140dibdnow9c7btw3z0ry2hiovfdas2sur0h2hu1m6ns" } }
转换要求
移除原JSON中的attachments/media_keys子段以及独立的includes子段,将attachments/media_keys替换为匹配到的preview_image_url和type字段。
转换前单条推文示例:
{ "attachments": { "media_keys": [ "16_1406870837026770949" ] }, "id": "1406870843972632579", "created_at": "2021-06-21T07:05:22.000Z", "text": "Working on a new storm chasing application with incoming thunder rumbling." }
转换后单条推文示例:
{ "preview_image_url": "https://pbs.twimg.com/tweet_video_thumb/E4Y1koXWUAU1Ls5.jpg", "type": "animated_gif", "id": "1406870843972632579", "created_at": "2021-06-21T07:05:22.000Z", "text": "Working on a new storm chasing application with incoming thunder rumbling." }
实现方案
处理思路
- 先将
includes.media数组转换为以media_key为键的映射表,避免每次查找媒体信息都遍历数组,提升处理效率 - 遍历
data数组中的每一条推文数据,对每条数据做如下处理:- 保留原有的
id、created_at、text等核心字段 - 如果当前推文存在
attachments.media_keys,就取第一个key到映射表中匹配对应的preview_image_url和type,直接合并到当前推文对象中 - 丢弃原有的
attachments字段,最终返回处理后的推文数组即可
- 保留原有的
完整代码实现
function transformTwitterResult(rawResult) { // 构建media_key到媒体信息的映射 const mediaMap = {} if (rawResult.includes?.media) { rawResult.includes.media.forEach(mediaItem => { mediaMap[mediaItem.media_key] = { preview_image_url: mediaItem.preview_image_url, type: mediaItem.type } }) } // 处理每条推文数据 const transformedData = rawResult.data.map(tweet => { // 复制所有原有字段 const processedTweet = { ...tweet } // 删除冗余的attachments字段 delete processedTweet.attachments // 匹配并合并媒体信息 if (tweet.attachments?.media_keys?.length) { const firstMediaKey = tweet.attachments.media_keys[0] const matchedMedia = mediaMap[firstMediaKey] if (matchedMedia) { Object.assign(processedTweet, matchedMedia) } } return processedTweet }) // 可按需保留meta字段,不需要的话直接返回transformedData即可 return { data: transformedData, meta: rawResult.meta } } // 调用方法 const rawResult = /* 替换为Twitter API返回的原始结果 */ const processedResult = transformTwitterResult(rawResult) // 打印格式化后的结果 console.log(JSON.stringify(processedResult, null, 2))
效果说明
上述代码处理后,带媒体附件的推文会直接新增preview_image_url和type字段,无附件的推文保留原有核心字段,完全匹配要求的转换效果。
内容的提问来源于stack exchange,提问作者Christoffer
相关产品推荐
相关产品推荐

