JSON 파일 통합
JSON 파일 통합
LangChain JavaScript로 JSON 파일 문서 로더와 통합하는 방법을 안내할게요.
출처: 문서
본문
JSON 로더는 JSON pointer를 사용해 JSON 파일에서 대상으로 삼을 키를 지정해요.
JSON pointer가 없는 예제
가장 간단한 사용법은 JSON pointer를 지정하지 않는 것이에요. 로더는 JSON 객체에서 찾은 모든 문자열을 로드할 거예요.
예제 JSON 파일:
{
"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."
}
}
예제 코드:
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 pointer 사용 예제
JSON 객체에서 문자열을 추출할 키를 선택할 수 있어요.
이 예제에서는 "from"과 "surname" 항목에서만 정보를 추출하려고 해요.
{
"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"
}
}
}
예제 코드:
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 }
}
]
*/
더 알아보기
- 이 문서를 MCP로 연결하면 Claude, VSCode 등에서 실시간 답변을 받을 수 있어요.
- GitHub에서 이 페이지 편집하기 또는 이슈 제출하기.