在 JavaScript 中解析 LinkedIn 个人资料 API 响应,先给它定义类型,再显式处理三件事:值为 null 的字段、值为 [] 的列表,以及 LinkedIn 的两种日期格式("2014" 和 "2014-02")。ProfileHunt 返回的每个键始终存在,所以你永远不需要判断键是否存在,只需判断它的值是否为 null。本文给出完整的 TypeScript 类型、处理日期和当前职位的辅助函数,以及一个把个人资料扁平化为一行表格数据的函数。
LinkedIn 个人资料 API 概述解释了数据从哪里来;本文从你拿到 JSON 之后讲起。
要点速览
- 把响应定义为以
status区分的联合类型。 检查status === "ok"之后,TypeScript 就知道profile不是null。 - 用
?.和??,不要用in。 键永远不会缺失;值可能是null,列表可能为空。 - 职位和学校的日期是字符串,不是时间戳。
"2014"和"2014-02"都会出现;new Date("2014")会凭空造出一个 1 月 1 日。 - 当前职位是
end_date === null的那一条。 可能不止一条,而且在公开视图中,过往职位没有title。
响应的 TypeScript 类型
这些类型依据个人资料端点参考和 ProfileHunt 首页上的示例响应整理。把它们粘贴到 profilehunt.ts 文件中:
export type Position = {
company: string;
company_url: string | null;
title: string | null; // 仅当前职位有值
location: string | null;
description: string | null;
start_date: string | null; // "2014" 或 "2014-02"
end_date: string | null; // null = 当前职位
};
export type School = {
school: string;
school_url: string | null;
start_date: string | null;
end_date: string | null;
};
export type Post = {
url: string;
posted_at: string; // ISO 8601 UTC
text: string | null;
likes: number | null;
};
export type Article = {
url: string;
headline: string | null;
image_url: string | null;
published_at: string; // ISO 8601 UTC
likes: number | null;
};
export type Reaction = {
verb: string; // 互动类型,沿用 LinkedIn 的标签
reacted_at: string; // ISO 8601 UTC
post_url: string | null;
post_author: string | null; // 从帖子 URL 中取得的个人资料名称
post_at: string | null;
};
export type Comment = {
verb: string; // "commented on a post" 或 "replied to a comment"
text: string | null;
age: string; // 与 LinkedIn 显示的一致:"5h"、"3d"、"2w"、"4mo"、"1y"
commented_at: string | null; // 近似值:从 scraped_at 往前推算 age
post_url: string | null;
post_author: string | null;
post_at: string | null; // 精确值
};
export type Profile = {
public_id: string;
profile_url: string;
full_name: string | null;
first_name: string | null;
last_name: string | null;
photo_url: string | null;
headline: string | null;
about: string | null;
location: string | null;
country: string | null; // 两位字母代码,例如 "US"
experience: Position[];
education: School[];
languages: string[];
followers: number | null;
connections: number | null; // 500 表示 500 或以上
creator: boolean;
badges: string[];
last_post_at: string | null;
recent_posts: Post[];
recent_articles: Article[];
last_reaction_at: string | null;
recent_reactions: Reaction[];
last_comment_at: string | null; // 近似值,与 commented_at 相同
recent_comments: Comment[];
last_active_at: string | null; // last_post_at、last_reaction_at、last_comment_at 中最新的一个
scraped_at: string; // ISO 8601 UTC
};
export type ProfileResult =
| { status: "ok"; profile: Profile }
| { status: "gone" | "unavailable"; profile: null };
这就是 API 返回的全部键。点赞和评论相关的键能反映出活跃度,即使对方从不发帖;commented_at 是近似值,因为 LinkedIn 只显示 4w 这样的相对时间,而 reacted_at 和 post_at 是精确值。个人资料参考对每个字段都有说明。
要拿到 ProfileResult,按在 Node.js 中调用 LinkedIn 个人资料 API 中的方式发送请求,再对解析后的响应体做类型断言:const result = (await res.json()) as ProfileResult。

最容易出错的字段
个人资料解析中的大多数 bug 来自四个地方:null、日期、当前职位和有上限的数字。每一个都有简短的解决办法。
null 与空列表
个人资料未公开显示的值是 null;没有内容的列表是 []。所以 profile.experience.length 永远安全,而 profile.experience[0] 可能是 undefined。
const name = profile.full_name ?? profile.public_id;
const firstSchool = profile.education[0]?.school ?? null;
const postCount = profile.recent_posts.length;
不要写 if ("headline" in profile)。它永远为真,反而掩盖了真正的问题:headline 是否为 null。
年份日期与年月日期
职位和学校上的 start_date 与 end_date 就是 LinkedIn 显示的内容:Bill Gates 在 Gates Foundation 的是 "2000",Satya Nadella 在 Microsoft 的是 "2014-02"。把它们解析成各个部分,而不是 Date:
export function parseYearMonth(d: string | null) {
if (!d) return null;
const [year, month] = d.split("-").map(Number);
return { year, month: month ?? null }; // month 为 1-12 或 null
}
new Date("2014") 返回的是 2014 年 1 月 1 日 UTC 零点,这个精度是个人资料从未声明过的。保留 month: null,再按使用场景决定,比如估算任职时长时“假设为年中”。而时间戳类字段(posted_at、published_at、reacted_at、last_post_at、last_active_at、scraped_at 等)都是完整的 UTC ISO 8601 字符串,对它们用 new Date(value) 是正确的。
直接对原始字符串排序是可行的:按普通字符串比较,"2014" < "2014-02" < "2015"。
当前职位
当前职位的 end_date 为 null。公开视图也只为当前职位显示 title;过往职位返回的是 title: null。
export function currentRoles(p: Profile) {
return p.experience.filter(e => e.end_date === null);
}
export const currentRole = (p: Profile) => currentRoles(p)[0] ?? null;
Satya Nadella 的示例个人资料有两个当前职位:自 "2014-02" 起在 Microsoft 担任 Chairman and CEO,以及自 "2018" 起在 University of Chicago 担任 Member Board Of Trustees。currentRole 取第一个,因为 experience 保留了个人资料中的排列顺序。如果你需要的是“雇主”,通常这样就对了;如果你需要所有任职关系,就用 currentRoles。
有上限的数字和很小的数字
connections 有上限:对未登录访客,LinkedIn 在超过 500 时显示“500+”,所以 500 表示 500 或以上。展示时写成 "500+"。很小的数字也是真实的:Bill Gates 的示例显示 connections: 8,而 followers: 40725248。衡量影响力请用 followers。
const connectionsLabel = (n: number | null) => (n === null ? "" : n >= 500 ? "500+" : String(n));

把个人资料扁平化为一行表格数据
电子表格、CSV 文件和 SQL 表都需要每人一行扁平数据。选出你需要的列,其余的推导出来:
export function toRow(p: Profile) {
const role = currentRole(p);
return {
public_id: p.public_id,
full_name: p.full_name ?? "",
headline: p.headline ?? "",
location: p.location ?? "",
country: p.country ?? "",
current_company: role?.company ?? "",
current_title: role?.title ?? "",
current_since: role?.start_date ?? "",
latest_school: p.education[0]?.school ?? "",
followers: p.followers ?? "",
connections: connectionsLabel(p.connections),
last_post_at: p.last_post_at ?? "",
scraped_at: p.scraped_at,
};
}
对 Satya Nadella 的示例运行 console.table([toRow(profile)]),得到:
| 列 | 值 |
|---|---|
full_name | Satya Nadella |
headline | Chairman and CEO at Microsoft |
location | Redmond, Washington, United States |
current_company | Microsoft |
current_title | Chairman and CEO |
current_since | 2014-02 |
latest_school | The University of Chicago Booth School of Business |
followers | 12222008 |
connections | 500+ |
last_post_at | 2026-10-12T13:03:00.729Z |
把原始 JSON 和扁平行放在一起保存(一个 jsonb 列,或一个 JSONL 文件)。扁平行用于阅读和筛选;有了 JSON,以后要加一列也无需重新获取个人资料。完整的“列表进、文件出”脚本见 Node.js 数据补全工作流。
大多数教程搞错了什么
把每个字段都建模为可选。 写成 headline?: string 等于告诉 TypeScript 这个键可能不存在,而它永远存在。headline: string | null 才准确,也会让编译器提出正确的问题。
所有日期都用 new Date() 解析。 这会把 "2014" 变成一个精确的时间戳,让任职时长的计算看起来很精确,实际上并非如此。
把 experience[0] 当作当前工作。 它只是列出的第一个职位,可能已经结束。先按 end_date === null 过滤。
误区与事实
| 误区 | 事实 |
|---|---|
| “TypeScript 类型会校验 API 响应。” | 类型在运行时就消失了。as ProfileResult 只是你做出的承诺,真正保证它的是运行时检查。 |
“experience 为空说明这个人没有工作。” | 这说明公开视图没有显示职位。很多会员会对未登录访客隐藏细节。 |
“旧职位的 title: null 是解析 bug。” | 公开视图不显示过往职位的头衔。null 就是正确的值。 |
“connections: 500 表示恰好 500。” | 它表示 500 或以上。 |
进阶:运行时检查与自动生成类型
只有一个服务时,手写类型就够了。当多个服务使用同一份数据时,有两项升级会很有帮助。
从 OpenAPI 规范生成类型。 ProfileHunt 在 /openapi.json 发布了它的 schema,其中 Profile、Position、School、Post、Article、Reaction 和 Comment 都是具名 schema。openapi-typescript 这类生成器可以把它转成类型,重新生成后,新字段就会出现在你的编辑器里。
在边界处检查,在内部信任。 在 JSON 进入你系统的地方校验一次:调用 API 的函数里,或读回已存储 JSON 的地方。一个这样小的守卫就能拦住大多数意外:
function isProfileResult(x: any): x is ProfileResult {
return (x?.status === "ok" && typeof x.profile?.public_id === "string")
|| ((x?.status === "gone" || x?.status === "unavailable") && x.profile === null);
}
Zod 这类 schema 库能提供同样的检查,并给出字段级别的错误。无论哪种方式,边界之后的所有代码都可以依赖这些类型。
ProfileHunt 的定位
ProfileHunt 是一个 LinkedIn 个人资料 API:输入公开的 LinkedIn 个人资料 URL,输出 JSON,每次响应的键都相同。正是这种稳定性让上面的解析代码如此简短:不需要检查键,一种 null 约定,一种日期约定。
每找到一份个人资料消耗 1 个点数,即每 1,000 份个人资料 0.50 美元,gone 和 unavailable 不收费。它只返回公开视图,所以没有电子邮件、没有电话号码、没有过往职位的头衔,connections 上限为 500。
开始使用
- 在 profilehunt.dev/login 登录:使用工作邮箱可获得 100 个免费点数,个人邮箱可获得 10 个。
- 获取一份个人资料,把 JSON 保存到文件。
- 粘贴上面的类型,对它运行
toRow,再对照 LinkedIn 个人资料 JSON 结构。用的是 Python?请看用 Python 解析个人资料 JSON。
想了解这个 API 是什么、涵盖哪些内容,请回到 LinkedIn 个人资料 API 概述。
常见问题
如何从 LinkedIn 个人资料 JSON 中获取某人的当前工作?
筛选 experience 中 end_date === null 的条目。第一条就是个人资料上排在最前面的职位;当前职位可能不止一个。
为什么 LinkedIn API 响应中过往工作的头衔是 null?
LinkedIn 的公开个人资料视图不显示过往职位的头衔,所以 ProfileHunt 对它们返回 title: null。公司和日期仍然存在。
如何把 "2014-02" 这样的 LinkedIn 日期转换成 JavaScript Date?
按 - 拆分成年份和可选的月份,而不是直接调用 new Date()。如果确实需要 Date,自己选定日期,例如 new Date(Date.UTC(year, (month ?? 1) - 1, 1)),并记住个人资料只提供了年份或年月。
LinkedIn 个人资料 API 有 TypeScript 类型吗?
你可以复制本文中的类型,也可以用 openapi-typescript 这类工具,从 ProfileHunt 位于 /openapi.json 的 OpenAPI 规范生成。
如何在 JavaScript 中把 LinkedIn 个人资料 JSON 转成 CSV?
把每份个人资料扁平化为一个只含字符串和数字的对象(姓名、标题、当前公司、关注者),然后每个对象写一行,对包含逗号或引号的值加上引号。


