Skip to content

Commit

Permalink
Add json loader (#1368)
Browse files Browse the repository at this point in the history
  • Loading branch information
collindutter authored Nov 22, 2024
1 parent 4195f1e commit ca5407c
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `GriptapeCloudAssistantDriver` for interacting with Griptape Cloud's Assistant API.
- `OpenAiAssistantDriver` for interacting with OpenAI's Assistant API.
- `GriptapeCloudToolTool` for running Griptape Cloud hosted Tools.
- `JsonLoader` for loading and parsing JSON files.

### Changed

Expand Down
9 changes: 9 additions & 0 deletions docs/griptape-framework/data/loaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ The Loader will load audio in its native format and populates the resulting Arti
--8<-- "docs/griptape-framework/data/src/loaders_10.py"
```

### JSON

Loads JSON files into [JsonArtifact](../../griptape-framework/data/artifacts.md#json)s:

```python

--8<-- "docs/griptape-framework/data/src/loaders_json.py"
```

## Web

!!! info
Expand Down
9 changes: 9 additions & 0 deletions docs/griptape-framework/data/src/loaders_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pathlib import Path

from griptape.loaders import JsonLoader

# Load an image from disk
JsonLoader().load("tests/resources/test.json")

# You can also pass a Path object
JsonLoader().load(Path("tests/resources/test.json"))
2 changes: 2 additions & 0 deletions griptape/loaders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .base_file_loader import BaseFileLoader

from .text_loader import TextLoader
from .json_loader import JsonLoader
from .pdf_loader import PdfLoader
from .web_loader import WebLoader
from .sql_loader import SqlLoader
Expand All @@ -19,6 +20,7 @@
"BaseLoader",
"BaseFileLoader",
"TextLoader",
"JsonLoader",
"PdfLoader",
"WebLoader",
"SqlLoader",
Expand Down
14 changes: 14 additions & 0 deletions griptape/loaders/json_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from __future__ import annotations

import json

from attrs import define

from griptape.artifacts import JsonArtifact
from griptape.loaders import BaseFileLoader


@define
class JsonLoader(BaseFileLoader[JsonArtifact]):
def parse(self, data: bytes) -> JsonArtifact:
return JsonArtifact(json.loads(data), encoding=self.encoding)
26 changes: 26 additions & 0 deletions tests/resources/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "John Doe",
"age": 30,
"email": "[email protected]",
"isActive": true,
"roles": [
"user",
"admin"
],
"address": {
"street": "123 Main St",
"city": "Anytown",
"state": "CA",
"postalCode": "12345"
},
"phoneNumbers": [
{
"type": "home",
"number": "555-555-5555"
},
{
"type": "work",
"number": "555-555-1234"
}
]
}
39 changes: 39 additions & 0 deletions tests/unit/loaders/test_json_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import pytest

from griptape.loaders import JsonLoader


class TestJsonLoader:
@pytest.fixture(params=["ascii", "utf-8", None])
def loader(self, request):
encoding = request.param
if encoding is None:
return JsonLoader()
else:
return JsonLoader(encoding=encoding)

@pytest.fixture(params=["path_from_resource_path"])
def create_source(self, request):
return request.getfixturevalue(request.param)

def test_load(self, loader, create_source):
source = create_source("test.json")

artifact = loader.load(source)

assert artifact.value["name"] == "John Doe"
assert artifact.encoding == loader.encoding

def test_load_collection(self, loader, create_source):
resource_paths = ["test.json"]
sources = [create_source(resource_path) for resource_path in resource_paths]

collection = loader.load_collection(sources)

keys = {loader.to_key(source) for source in sources}
assert collection.keys() == keys

key = next(iter(keys))
artifact = collection[key]

assert artifact.encoding == loader.encoding

0 comments on commit ca5407c

Please sign in to comment.