Python - I would like some examples of using "APLICATION/LD+JSON"

Asked

Viewed 77 times

-4

I’ve always worked with REST for integrations, now I’m using a new system where I can’t use only pure JSON, they use LD Json, and I still don’t quite understand this concept and I’m having some difficulties to implement.

So my question is half vague, because I still don’t know exactly what I should look for, I don’t understand how the structure of Json is made, and how I mount it... Or how Header looks now.

I’m trying to understand the Library PYLB but tense...

  • Hello Critopher, since you’re new here in the community, how about tour welcome? So you will get guidance on how to ask questions that some member can help you in a practical way.

1 answer

1


JSON-LD is nothing more than a JSON, obviously its keys follow a specified format.

Usually it is embedded in HTML pages, something like:

<script type="application/ld+json">
{
  "@context": "http://json-ld.org/contexts/person.jsonld",
  "@id": "http://dbpedia.org/resource/John_Lennon",
  "name": "John Lennon",
  "born": "1940-10-09",
  "spouse": "http://dbpedia.org/resource/Cynthia_Lennon"
}
</script>

The @context indicates the type of content you want to inform it contains, usually it is used to assist searchers, bots and the like to understand the content of the current page.

Of course, it can be used for more than this, but I assume that this will depend on your need, for example if you want to define a specific format for your Rests, this will probably come in handy as well, since each answer will have to have the format following exactly the "context" defined.

The use of JSON-LD is to make it easy to read some content, since it will follow a "norm" (a "context"). Assuming you will use somewhere specific, the use of Pyld even seems to me opicional, since understanding the 'simplicity' and the basic https://json-ld.org/spec/latest/json-ld/ you can even do it in "hand" (with Django or Flask, assuming you are developing for WEB).

The Pyld is just to facilitate even, it will not do all its work, as the HTTP part of your Rest, this who will do it is you or the framework that chose to develop the REST, see an example with Flask (I did in this FW because it is what I currently use):

from flask import Flask, request
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

todos = {}

class Pessoa(Resource):
    def get(self, id):
        data = '...aqui viria a resposta do banco ...';

        return {
            "@context": "http://json-ld.org/contexts/person.jsonld",
            "@id": "http://site.com/pessoa/" + id,
            "name": data.name,
            "born": data.birth
        }

api.add_resource(Pessoa, '/pessoa/<string:id>')

if __name__ == '__main__':
    app.run(debug=True)

I can’t tell if it’s mandatory to wear the content-type as application/ld+json in the HTTP response, but assuming it is, then you can set the default in the default_config

app = Flask(__name__)
app.config['JSONIFY_MIMETYPE'] = 'application/ld+json'

api = Api(app)

But I believe that this is relative, it will depend on the "client" (software) that will access the REST.

  • I get it. Thank you...

  • @Cristophergollmann left an example in Flask very humble just to understand more or less the proposal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.