Bird
0
0

You need to build a custom loader that reads multiple JSON files from a directory, extracts the 'content' field, and returns Document objects with metadata including filename. Which approach is best?

hard📝 Application Q8 of 15
LangChain - Document Loading
You need to build a custom loader that reads multiple JSON files from a directory, extracts the 'content' field, and returns Document objects with metadata including filename. Which approach is best?
ARead all files as raw text, concatenate into one string, and return a single Document
BIterate over files, parse JSON, create Document(content=content, metadata={'filename': filename}) for each, and return list
CLoad files using built-in TextLoader without metadata, then manually add metadata later
DReturn a dictionary mapping filenames to content strings instead of Document objects
Step-by-Step Solution
Solution:
  1. Step 1: Read each JSON file

    Parse JSON to extract 'content' field.
  2. Step 2: Create Document objects

    Use Document(content=content, metadata={'filename': filename}) for each file.
  3. Step 3: Return list of Documents

    Return a list of these Document objects as required by Langchain.
  4. Final Answer:

    Iterate over files, parse JSON, create Document(content=content, metadata={'filename': filename}) for each, and return list -> Option B
  5. Quick Check:

    Each file becomes a Document with metadata [OK]
Quick Trick: Parse JSON, create Document with metadata, return list [OK]
Common Mistakes:
  • Concatenating all files into one Document
  • Using TextLoader without metadata
  • Returning dict instead of list of Documents

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes