简体   繁体   中英

JSONLD: How to convert a json into JsonLD?

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.

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"
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM