RE: JSON-LD from Turtle/N-Triples in JS

Hi Adrian,

On Saturday, January 18, 2014 6:43 PM, Adrian Gschwend wrote:
> 
> Googling didn't help so I ask here: I can parse Turtle/N-Triples with
> Rubens N3.js or some other libraries like rdfstore (which use N3
> again).
> Now the question is how do I serialize JSON-LD out of it, anyone knows
> of a direct way?

I assume you are looking for something in JavaScript, right. If so, you
should have a look at Dave's JSON-LD parser:

  https://github.com/digitalbazaar/jsonld.js

It is able to parse N-Quads (and thus also N-Triples) and turn that into
JSON-LD.


> If not, what's the easiest JSON-LD structure I would
> have to generate to get there?

The absolute easiest thing is to create an array containing your triples
like so:

[
  { "@id": "http://i-am-the-subject.com/", "http://and.me/the-prop": "with a
literal value" },
  { "@id": "subj", "prop": { "@id": "http://an.object.me/" } },
  { "@id": "subj", "prop": { "@value": "typed literal", "@type":
"http://me.type.com" } }
]

If you want to avoid the top-level array, just specify the default graph
explicitely:

{
  "@graph": [ ... array as above ]
}


HTH,
Markus


--
Markus Lanthaler
@markuslanthaler

Received on Saturday, 18 January 2014 17:53:44 UTC