Comments (3)
Hi @scisci,
It looks like a good case to use JSON-LD Framing.
See examples in the official test suite:
https://github.com/kazarena/json-gold/blob/master/ld/testdata/frame-0001-in.jsonld
https://github.com/kazarena/json-gold/blob/master/ld/testdata/frame-0001-frame.jsonld
https://github.com/kazarena/json-gold/blob/master/ld/testdata/frame-0001-out.jsonld
In this example we have a library, a book and a chapter:
{
"@context": {
"dc": "http://purl.org/dc/elements/1.1/",
"ex": "http://example.org/vocab#",
"ex:contains": {"@type": "@id"}
},
"@graph": [
{
"@id": "http://example.org/test/#library",
"@type": "ex:Library",
"ex:contains": "http://example.org/test#book"
},
{
"@id": "http://example.org/test#book",
"@type": "ex:Book",
"dc:contributor": "Writer",
"dc:title": "My Book",
"ex:contains": "http://example.org/test#chapter"
},
{
"@id": "http://example.org/test#chapter",
"@type": "ex:Chapter",
"dc:description": "Fun",
"dc:title": "Chapter One"
}
]
}
And we would like to ensure that chapters are nested within books and books are nested within libraries. So we need to invoke frame operation with the following frame document:
{
"@context": {
"dc": "http://purl.org/dc/elements/1.1/",
"ex": "http://example.org/vocab#"
},
"@type": "ex:Library",
"ex:contains": {
"@type": "ex:Book",
"ex:contains": {
"@type": "ex:Chapter"
}
}
}
It will then give us the following output:
{
"@context": {
"dc": "http://purl.org/dc/elements/1.1/",
"ex": "http://example.org/vocab#"
},
"@graph": [{
"@id": "http://example.org/test/#library",
"@type": "ex:Library",
"ex:contains": {
"@id": "http://example.org/test#book",
"@type": "ex:Book",
"dc:contributor": "Writer",
"dc:title": "My Book",
"ex:contains": {
"@id": "http://example.org/test#chapter",
"@type": "ex:Chapter",
"dc:description": "Fun",
"dc:title": "Chapter One"
}
}
}]
}
I believe you can solve your problem by using the same approach. However, as always, it's worth mentioning that JSON-LD is just JSON in the end and sometimes framing is a way too heavy solution for simple problems. I.e. if you are in full control of the input data and if its structure is reasonably static, it may be easier just to nest the nodes directly in JSON rather than relying on powerful but relatively expensive operations like framing.
See more discussion here
from json-gold.
Yep that looks like what I was looking for, although I would also prefer to get rid of the graph property and have it as a single object. Thanks again for the help.
PS in the meantime I wrote some code that does the same thing (finds ids and nests, reducing down to a single object) but the framing method is probably more robust.
from json-gold.
Graph property is not required in the example above (because there's just one node inside) but it may be necessary for other documents. I can't remember exactly but there may be a configuration option to remove the graph in this case.
Based on my experience so far with JSON-LD, if there is a requirement to have your documents in a specific (e.g. pretty or minimalist) shape, you will most likely need to do some small post-processing of your JSON-LD document after applying standard transformations.
from json-gold.
Related Issues (13)
- Blank nodes across graphs / RDF lists HOT 1
- Examples showing how to build a graph HOT 3
- Frame processing of lists appears to be broken HOT 15
- Expand a compact document HOT 3
- Frame adds an extra empty string element to some singleton sets HOT 6
- Schema Validation HOT 2
- How to use multiple context in compact and expand functions HOT 6
- Using JSON-LD string in toRDF HOT 5
- URDNA2015 Normalization HOT 4
- How to get framing results into struct HOT 8
- Releases + Tags HOT 3
- Change of maintainer (Dec 2017) HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-gold.