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

-4

I've always worked with REST for integrations, now I'm using a new system where I can not just use pure JSON, they use LD Json, and I still do not quite understand this concept and I'm having some difficulties implementing it. So my question is a bit vague, because I still do not know exactly what to look for, I do not understand how Json's structure is made, and how I mount it ... Not even the header is now.

I'm trying to understand the Library PYLB but it's tense ...

    
asked by anonymous 07.05.2018 / 19:58

1 answer

1

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

Generally it's 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 report it contains, usually it is used to help search engines, bots and the like to understand the content of the current page.

Of course it can serve more than this, but I assume this depends on your need, for example if you want to define a specific format for your RESTs, this will probably come in handy, since each response it will have to be formatted exactly by the "context" .

The use of JSON-LD is to make it easy to read some content, since it will follow a "norm" (a "context"). Assuming it will use somewhere specific, the use of PyLD even seems to me optional, since understanding 'simplicity' and the basics #

PyLD is just to make it even easier, it will not do all your work, like the HTTP part of your Rest, this will do it yourself or the framework you chose to develop REST, see an example with Flask (I did this in FW because that's 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 not tell if it is mandatory to use content-type as application/ld+json in the HTTP response, but assuming it is, then you can set the default in default_config

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

api = Api(app)

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

    
07.05.2018 / 21:11