-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature ideas] JSON-LD #283
Comments
You can transform your JSON-like structure into triplet format within PostgreSQL, and you can retrieve it in a way that suits your needs: Assume you have this JSON: {
"@id": "person:123",
"@type": "Person",
"name": "Alice",
"knows": {"@id": "person:456"}
} And you need to convert it to:
Let's Start:# Step 1: Create an Example DB CREATE TABLE people (
data JSONB NOT NULL
); # Step 2: Insert Your Data INSERT INTO people (data)
VALUES
('{"@id": "person:123", "@type": "Person", "name": "Alice", "knows": {"@id": "person:456"}}'); # Step 3: Run the Below Code and Get Your Result import psycopg2
# Connect to your PostgreSQL database
connection = psycopg2.connect(
dbname="your_database",
user="your_user",
password="your_password",
host="localhost"
)
cursor = connection.cursor()
# Execute the query to extract triplets
cursor.execute("""
WITH triplets AS (
SELECT
data->>'@id' AS subject,
'@type' AS predicate,
data->>'@type' AS object
FROM people
UNION ALL
SELECT
data->>'@id' AS subject,
'name' AS predicate,
data->>'name' AS object
FROM people
UNION ALL
SELECT
data->>'@id' AS subject,
'knows' AS predicate,
data->'knows'->>'@id' AS object
FROM people
)
SELECT * FROM triplets;
""")
# Fetch and format the results
for row in cursor.fetchall():
print(f'("{row[0]}", "{row[1]}", "{row[2]}").')
# Close the connection
cursor.close()
connection.close() ResultRunning the code will produce the output:
This approach allows you to easily convert and retrieve data from a JSON structure as triplets in PostgreSQL. |
Hi @a-teammate You can take a JSON-LD and convert it into triples or quads. Then you can store them in Cozo by creating a relation where the columns subject, predicate, object (and graph name) are all part of the key. Using CozoScript you can then query the relation and construct a JSON-LD back. Depending on query complexity you might need to actually return the set of triples/quads and build the JSON-LD representation in your client programming language. For the queries to be performant you will need to create several covering indices with different collation orders eg POS is you want to find subjects by predicate, or {predicate, object} I am doing exactly this for several projects. |
Hey there, thanks for the awesome library you built there.
It will be a noob issue, and noob view I make here.
As a newbie to datalog, i have troubles modeling my data as triplets.
Usually I formulate my data as some kind of tree, then i figure where deduplication is necessary to make the things in the domain be consistent (and pull out things of the tree to glue them back together with IDs).
I don't like graphql and those, because they make me code all lookups/relations, which feel like a constant "hoping for the best".
But I like how it gives me a tree on the API level.
I like how cozodb works even for embedded deploys and the advanced features for all the fancy community detection and sorting algos (I want to generate "see also" sections in my UI, so this will come in handy. And other graph stuff).
Just because I struggle a bit with wrapping my head around cozoscript queries, is there any chance there could be something like https://json-ld.org/ for cozo?
Or do you maybe even an idea how that would map to a (lower-level) cozoscript API?
rdflib and pyld do this for python, but I want to get the cozodb graph algo goodies! :D
Or would it make more sense to use them on-top somehow?
Thank you very much, if you find the time to shed some light onto this!
The text was updated successfully, but these errors were encountered: