Skip to main content

A Python library to automatically import JSON to FalkorDB as a Graph

Project description

license Release PyPI version Codecov Forum Discord

json2graph

Try Free

A Python library to automatically import JSON to FalkorDB as a Graph.

Overview

json2graph converts JSON data (from files or dictionaries) into a graph structure in FalkorDB. It automatically creates nodes from objects and arrays with smart labeling based on keys, extracts primitive values as properties, and creates relationships based on the JSON structure. The library handles nested data recursively and prevents duplicate nodes using content hashing.

Features

  • 🔄 Automatic Graph Creation: Converts JSON objects and arrays into graph nodes
  • 🏷️ Smart Labeling: Uses JSON keys to label nodes intelligently
  • 🔗 Relationship Mapping: Creates relationships based on JSON structure
  • 📦 Property Extraction: Extracts primitive values (strings, numbers, booleans) as node properties
  • 🔁 Recursive Processing: Handles deeply nested JSON structures
  • 🔒 Duplicate Prevention: Uses content hashing to prevent duplicate nodes
  • 🗂️ File & Dict Support: Import from JSON files or Python dictionaries
  • 🧹 Database Management: Optional database clearing before import
  • 🔌 FalkorDB Integration: Uses FalkorDB Python client with Cypher queries via GRAPH.QUERY

Installation

Using uv (recommended)

uv pip install -e .

Using pip

pip install .

Or install dependencies directly:

pip install falkordb

Quick Start

from json2graph import JSONImporter

# Initialize the importer
importer = JSONImporter(
    host="localhost",
    port=6379,
    graph_name="my_graph"
)

# Import from a dictionary
data = {
    "name": "John Doe",
    "age": 30,
    "skills": ["Python", "JavaScript"]
}
importer.convert(data, clear_db=True)

# Import from a JSON file
importer.load_from_file("data.json", clear_db=True)

Usage

Initialize the Importer

from json2graph import JSONImporter

importer = JSONImporter(
    host="localhost",      # FalkorDB host (default: "localhost")
    port=6379,            # FalkorDB port (default: 6379)
    graph_name="my_graph" # Graph database name (default: "json_graph")
)

Convert JSON Dictionary

# Simple object
data = {
    "product": "Laptop",
    "price": 999.99,
    "in_stock": True
}
importer.convert(data, clear_db=True, root_label="Product")

Load from JSON File

# Load and convert JSON file
importer.load_from_file("data.json", clear_db=True)

Nested Structures

The library handles nested objects and arrays automatically:

data = {
    "company": "TechCorp",
    "employees": [
        {
            "name": "Alice",
            "role": "Developer",
            "skills": ["Python", "Go"]
        },
        {
            "name": "Bob",
            "role": "Designer",
            "skills": ["Photoshop"]
        }
    ],
    "location": {
        "city": "San Francisco",
        "country": "USA"
    }
}
importer.convert(data, root_label="Company")

Clear Database

# Clear all nodes and relationships
importer.clear_db()

# Or clear during import
importer.convert(data, clear_db=True)
importer.load_from_file("data.json", clear_db=True)

How It Works

  1. Node Creation:

    • JSON objects become nodes with labels derived from their keys
    • JSON arrays become container nodes with element relationships
    • Primitive values become node properties
  2. Smart Labeling:

    • Object nodes are labeled based on their parent key
    • Array nodes get "Array" suffix (e.g., "employeesArray")
    • Labels are sanitized for Cypher compatibility
  3. Relationships:

    • Parent-child relationships are created based on JSON structure
    • Relationship types are derived from JSON keys
    • Array elements get indexed relationships (e.g., "ELEMENT_0", "ELEMENT_1")
  4. Duplicate Prevention:

    • Content hashing (SHA256) identifies duplicate nodes
    • Identical content creates only one node in the graph
    • Cache prevents redundant database queries
  5. Cypher Execution:

    • All operations use FalkorDB's GRAPH.QUERY command
    • Cypher queries are generated automatically
    • Transactions ensure data consistency

API Reference

JSONImporter

__init__(host="localhost", port=6379, graph_name="json_graph")

Initialize the JSON Importer.

Parameters:

  • host (str): FalkorDB host address
  • port (int): FalkorDB port number
  • graph_name (str): Name of the graph database

convert(data, clear_db=False, root_label="Root")

Convert JSON data into a graph structure.

Parameters:

  • data (Union[Dict, List, Any]): JSON data as dict, list, or primitive value
  • clear_db (bool): If True, clear the database before importing
  • root_label (str): Label for the root node

load_from_file(filepath, clear_db=False)

Load JSON data from a file and import it into the graph.

Parameters:

  • filepath (str): Path to the JSON file
  • clear_db (bool): If True, clear the database before importing

Raises:

  • FileNotFoundError: If the file doesn't exist
  • ValueError: If the file contains invalid JSON

clear_db()

Clear all data from the current graph database.

Examples

See the examples/ directory for more detailed examples:

  • basic_usage.py: Simple usage examples with various JSON structures

Testing

Run the test suite:

python -m pytest tests/

Or using unittest:

python -m unittest discover tests

Requirements

  • Python >= 3.8
  • falkordb >= 4.0.0
  • FalkorDB server running and accessible

License

MIT License - see LICENSE file for details

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

For issues and questions, please open an issue on the GitHub repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json2graph-0.1.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json2graph-0.1.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file json2graph-0.1.0.tar.gz.

File metadata

  • Download URL: json2graph-0.1.0.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for json2graph-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3b084aface58bb13b77f53f7a40743eefce509be41d0996d5174762f2fc60ccc
MD5 e83b87fd97ab0e6befc05c0f8d20881c
BLAKE2b-256 bc86fe8df91930c7266a81aced7d523d84bc7fb7c3017af79fad5ce2cefd4c52

See more details on using hashes here.

File details

Details for the file json2graph-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: json2graph-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for json2graph-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4757b9d6f594485ad1fead72be106cd70ee9455675d1ffbe6a3b818aca9ef13e
MD5 32f334441343c2107328848fa90322a6
BLAKE2b-256 adea946245262eb1c9daa9b57a1d4ffcb955e012fc8a33c6552698484dbccf7d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page