3

I have a use case where I have a Json data and I have to convert that into JSONLD format.

First Question : Can this be done easily , like some API for this, that probably I am missing ?

Second Question : If not then what are the steps that needs to be taken.

So the Json Looks like :

{
key:"language",
value: "scala"
}

And I want to convert it into the JSONLD format.

Any help is appreciated.

Shivansh
  • 3,454
  • 23
  • 46
  • See: https://stackoverflow.com/questions/43219064/an-code-example-of-serialization-json-to-json-ld-in-java – jschnasse Apr 05 '18 at 13:35

2 Answers2

2

You can simply add a context on this json object like :

{
  @context: {
        "key": "http://schema.org/description",
        "value": "http://schema.org/value"
      },
  key: "language",
  value: "scala"
}
dj_1993
  • 93
  • 1
  • 8
1

If you are interested in using JavaScript library to do this task, then npm module JSONLD is great one. Note: This library has dependency on PYTHON.

JavaScript code Example from JSONLD site:

var doc = {
  "http://schema.org/name": "Manu Sporny",
  "http://schema.org/url": {"@id": "http://manu.sporny.org/"},
  "http://schema.org/image": {"@id": "http://manu.sporny.org/images/manu.png"}
};
var context = {
  "name": "http://schema.org/name",
  "homepage": {"@id": "http://schema.org/url", "@type": "@id"},
  "image": {"@id": "http://schema.org/image", "@type": "@id"}
};

// compact a document according to a particular context
// see: http://json-ld.org/spec/latest/json-ld/#compacted-document-form
jsonld.compact(doc, context, function(err, compacted) {
  console.log(JSON.stringify(compacted, null, 2));
});

Output:

 {
    "@context": {...},
    "name": "Manu Sporny",
    "homepage": "http://manu.sporny.org/",
    "image": "http://manu.sporny.org/images/manu.png"
  }

When I was searching for JavaScript library for the JSON to JSONLD transformation, I could come up with this one. But, as this library has PYTHON dependency, I am searching for other JavaScript library for the same.

Kishor Jadhav
  • 196
  • 2
  • 16