> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# JSON 文件集成

> 使用 LangChain JavaScript 与 JSON 文件文档加载器集成。

JSON 加载器使用 [JSON 指针](https://github.com/janl/node-jsonpointer) 来定位您想要在 JSON 文件中提取的键。

### 无 JSON 指针示例

最简单的使用方式是不指定 JSON 指针。
加载器将加载它在 JSON 对象中找到的所有字符串。

示例 JSON 文件：

```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{
  "texts": ["This is a sentence.", "This is another sentence."],
  "nestedTexts": {
    "one": "This is a sentence nested in an object.",
    "two": "This is another sentence nested in an object."
  }
}
```

示例代码：

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { JSONLoader } from "@langchain/classic/document_loaders/fs/json";

const loader = new JSONLoader("src/document_loaders/example_data/example.json");

const docs = await loader.load();
/*
[
  Document {
    pageContent: 'This is a sentence.',
    metadata: { source: 'example.json', line: 1 }
  },
  Document {
    pageContent: 'This is another sentence.',
    metadata: { source: 'example.json', line: 2 }
  },
  Document {
    pageContent: 'This is a sentence nested in an object.',
    metadata: { source: 'example.json', line: 3 }
  },
  Document {
    pageContent: 'This is another sentence nested in an object.',
    metadata: { source: 'example.json', line: 4 }
  }
]
*/
```

### 使用 JSON 指针示例

您可以选择要从 JSON 对象中的哪些键提取字符串。

在此示例中，我们只想从 "from" 和 "surname" 条目中提取信息。

```json theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
{
  "1": {
    "body": "BD 2023 SUMMER",
    "from": "LinkedIn Job",
    "labels": ["IMPORTANT", "CATEGORY_UPDATES", "INBOX"]
  },
  "2": {
    "body": "Intern, Treasury and other roles are available",
    "from": "LinkedIn Job2",
    "labels": ["IMPORTANT"],
    "other": {
      "name": "plop",
      "surname": "bob"
    }
  }
}
```

示例代码：

```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import { JSONLoader } from "@langchain/classic/document_loaders/fs/json";

const loader = new JSONLoader(
  "src/document_loaders/example_data/example.json",
  ["/from", "/surname"]
);

const docs = await loader.load();
/*
[
  Document {
    pageContent: 'LinkedIn Job',
    metadata: { source: 'example.json', line: 1 }
  },
  Document {
    pageContent: 'LinkedIn Job2',
    metadata: { source: 'example.json', line: 2 }
  },
  Document {
    pageContent: 'bob',
    metadata: { source: 'example.json', line: 3 }
  }
]
```

***

<div className="source-links">
  <Callout icon="terminal-2">
    [通过 MCP 将这些文档](/use-these-docs)连接到 Claude、VSCode 等，以获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/oss/javascript/integrations/document_loaders/file_loaders/json.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
