# JSONLoader

> **Class** in `langchain_community`

📖 [View in docs](https://reference.langchain.com/python/langchain-community/document_loaders/json_loader/JSONLoader)

Load a `JSON` file using a `jq` schema.

## Signature

```python
JSONLoader(
    self,
    file_path: Union[str, PathLike],
    jq_schema: str,
    content_key: Optional[str] = None,
    is_content_key_jq_parsable: Optional[bool] = False,
    metadata_func: Optional[Callable[[Dict, Dict], Dict]] = None,
    text_content: bool = True,
    json_lines: bool = False,
)
```

## Description

**Setup:**

.. code-block:: bash

pip install -U jq

**Instantiate:**

.. code-block:: python

from langchain_community.document_loaders import JSONLoader
import json
from pathlib import Path

file_path='./sample_quiz.json'
data = json.loads(Path(file_path).read_text())
loader = JSONLoader(
         file_path=file_path,
         jq_schema='.quiz',
         text_content=False)

**Load:**

.. code-block:: python

    docs = loader.load()
    print(docs[0].page_content[:100])
    print(docs[0].metadata)

.. code-block:: python

    {"sport": {"q1": {"question": "Which one is correct team name in
    NBA?", "options": ["New York Bulls"
    {'source': '/sample_quiz
    .json', 'seq_num': 1}

**Async load:**

.. code-block:: python

    docs = await loader.aload()
    print(docs[0].page_content[:100])
    print(docs[0].metadata)

.. code-block:: python

    {"sport": {"q1": {"question": "Which one is correct team name in
    NBA?", "options": ["New York Bulls"
    {'source': '/sample_quizg
    .json', 'seq_num': 1}

**Lazy load:**

.. code-block:: python

    docs = []
    docs_lazy = loader.lazy_load()

    # async variant:
    # docs_lazy = await loader.alazy_load()

    for doc in docs_lazy:
        docs.append(doc)
    print(docs[0].page_content[:100])
    print(docs[0].metadata)

.. code-block:: python

    {"sport": {"q1": {"question": "Which one is correct team name in
    NBA?", "options": ["New York Bulls"
    {'source': '/sample_quiz
    .json', 'seq_num': 1}

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `file_path` | `Union[str, PathLike]` | Yes | The path to the JSON or JSON Lines file. |
| `jq_schema` | `str` | Yes | The jq schema to use to extract the data or text from the JSON. |
| `content_key` | `str` | No | The key to use to extract the content from the JSON if the jq_schema results to a list of objects (dict). If is_content_key_jq_parsable is True, this has to be a jq compatible schema. If is_content_key_jq_parsable is False, this should be a simple string key. (default: `None`) |
| `is_content_key_jq_parsable` | `bool` | No | A flag to determine if content_key is parsable by jq or not. If True, content_key is treated as a jq schema and compiled accordingly. If False or if content_key is None, content_key is used as a simple string. Default is False. (default: `False`) |
| `metadata_func` | `Callable[Dict, Dict]` | No | A function that takes in the JSON object extracted by the jq_schema and the default metadata and returns a dict of the updated metadata. (default: `None`) |
| `text_content` | `bool` | No | Boolean flag to indicate whether the content is in string format, default to True. (default: `True`) |
| `json_lines` | `bool` | No | Boolean flag to indicate whether the input is in JSON Lines format. (default: `False`) |

## Extends

- `BaseLoader`

## Constructors

```python
__init__(
    self,
    file_path: Union[str, PathLike],
    jq_schema: str,
    content_key: Optional[str] = None,
    is_content_key_jq_parsable: Optional[bool] = False,
    metadata_func: Optional[Callable[[Dict, Dict], Dict]] = None,
    text_content: bool = True,
    json_lines: bool = False,
)
```

| Name | Type |
|------|------|
| `file_path` | `Union[str, PathLike]` |
| `jq_schema` | `str` |
| `content_key` | `Optional[str]` |
| `is_content_key_jq_parsable` | `Optional[bool]` |
| `metadata_func` | `Optional[Callable[[Dict, Dict], Dict]]` |
| `text_content` | `bool` |
| `json_lines` | `bool` |


## Properties

- `jq`
- `file_path`

## Methods

- [`lazy_load()`](https://reference.langchain.com/python/langchain-community/document_loaders/json_loader/JSONLoader/lazy_load)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-community/blob/4b280287bd55b99b44db2dd849f02d66c89534d5/libs/community/langchain_community/document_loaders/json_loader.py#L11)