How to present empty relationship in JSONAPI? - json-api

Looked at http://jsonapi.org/format/ but don't see any description about empty relationship format, for example:
{
"type": "articles",
"id": "1",
"attributes": {
"title": "Rails is Omakase"
},
"relationships": {
"comments": {
"data": []
}
}
}
This article doesn't have comments, what is the correct way to present a empty relationship?
"data": [] or "data": null or no "relationships" at all?
Thanks!

It's described in resource linkage chapter of JSON API specification:
Resource linkage MUST be represented as one of the following:
null for empty to-one relationships.
an empty array ([]) for empty to-many relationships.
a single resource identifier object for non-empty to-one relationships.
an array of resource identifier objects for non-empty to-many relationships.
Note that you don't have to use resource linkage at all. You could also use relationship links. You could find more information about that one in this part of specification.

Related

In Geocoder here-api what are the different types of View

I am moving from Google to Here-api Geocoding service.In the Response JSON object there is a property View which is an array. I was not able to find in the documentation what types of objects might be in this collection. It seems that there is a type-_type property for every View and from all my testing I got only one object inside this array and it is always from the same type "_type": "SearchResultsViewType". My questions are:
Is it possible this collection that View collection to contain more than one object? Are they from the same "_type" or from a different "_type"?In short, besides probably ViewId how are different Views objects separated?
I made numerous requests to Geogoder API with switching on and off different parameters. I have searched here and in the API documentation.
This is how the Response object is looking now
{
"Response": {
"MetaInfo": {
"Timestamp": "2019-04-18T11:48:31.306+0000"
},
"View": [
{ "_type": "SearchResultsViewType",
"ViewId": 0,
"Result": .....

Swagger with swag gem - response of an array of multiple complex types

Swagger docs say that we can use a complex type object specified as array type as follows:
"type": "array",
"items": {
"$ref": "#/components/schemas/VeryComplexType"
}
But what if you have an array of different complex types (e.g. country, address, etc.) ? When trying to pass multiple $ref as follows:
"type": "array",
"items": {
"$ref": "#/components/schemas/VeryComplexType-1"
"$ref": "#/components/schemas/VeryComplexType-2"
"$ref": "#/components/schemas/VeryComplexType-3"
}
raises an error:
warning: key :$ref is duplicated and overwritten on line 245
warning: key :$ref is duplicated and overwritten on line 246
and it keeps the last one only. How is ti possible to fix that (if it is) ?
Thank you.
It is available starting from Open API 3.0 and it was discussed at this issue.
As rswag supports Swagger 2.0 only, it is not possible.

Freebase not returning all available values

I'm trying to extract the link to the NYTimes Topic page (among the topic_equivalent_webpage values) for Barack Obama from Freebase, but my query doesn't return any results, although it's on the webpage (http://www.freebase.com/m/02mjmr). This is my query:
[{
"id": "/en/barack_obama",
"type": "/common/topic",
"topic_equivalent_webpage": {
"value": null,
"value~=": "*nytimes*"
}
}]
I've also tried extracting all of the topic_equivalent_webpage values instead, using this query:
[{
"id": "/en/barack_obama",
"type": "/common/topic",
"topic_equivalent_webpage": []
}]
For some reason it only returns one of the values (http://www.worldcat.org/wcidentities/lccn-n94-112934).
Does anyone have any tips?
NOTE: All Freebase APIs are going away in a few months.
You have three choices:
Download the RDF dump and filter it. This is most appropriate for a large-scale download instead of using the API. For the property name and decoding process, see #3.
Use the Topic API i.e. https://www.googleapis.com/freebase/v1/topic/en/barack_obama?filter=/common/topic/topic_equivalent_webpage
Query MQL for the keys in the namespace that you want (ie the NY Times namespace)
[{
"id": "/en/barack_obama",
"key": [{
"namespace": "/source/nytimes",
"value": null
}]
}]
Normally the result is an identifier that gets substituted into a URI template, but in the NYT case it's basically a full URI path that just gets appended to http://nytimes.com/
The key value (e.g. top$002Freference$002Ftimestopics$002Fpeople$002Fo$002Fbarack_obama) will be MQL key encoded, so they'll need to be decoded, but in this case you can probably cheat and replace all "$002F" substrings with "/". If any other characters are encoded, just replace $dddd with the character that has that Unicode code point.

"Reverse formatting" Riak search results

Let's say I have an object in the test bucket in my Riak installation with the following structure:
{
"animals": {
"dog": "woof",
"cat: "miaow",
"cow": "moo"
}
}
When performing a search request for this object, the structure of the search results is as follows:
{
"responseHeader": {
"status": 0,
"QTime": 3,
"params": {
"q": "animals_cow:moo",
"q.op": "or",
"filter":"",
"wt": "json"
}
},
"response": {
"numFound": 1,
"start": 0,
"maxScore": "0.353553",
"docs": [
{
"id": "test",
"index": "test",
"fields": {
"animals_cat": "miaow",
"animals_cow": "moo",
"animals_dog": "woof"
},
"props": {}
}
]
}
}
As you can see, the way the object is stored, the cat, cow and dog keys are nested within animals. However, when the search results come back, none of the keys are nested, and are simply separated by _.
My question is this: Is there any way provided by Riak to "reverse format" the search, and return the fields of the object in the correct (nested) format? This becomes a problem when storing and returning user data that might possibly contain _.
I do see that the latest version of Riak (beta release) provides a search schema, but I can't seem to see whether my question would be answered by this.
What you receive back in the search result is what the object looked like after passing through the json analyzer. If you need the data formatted differently, you can use a custom analyzer. However, this will only affect newly put data.
For existing data, you can use the id field and issue a get request for the original object, or use the solr query as input to a MapReduce job.

Freebase Obtain All Information On One Subject

I'm trying to find the best way to get the information displayed on a Freebase page via a MQL query.
I've tried the topic API but that includes a lot of metadata.
I've also tried using links/reflection as in:
{
"id": "/en/samsung_electronics",
"/type/reflect/any_master": [{
"link": {
"master_property": null
},
"name": null,
"id": null
}],
"/type/reflect/any_reverse": [{
"link": {
"master_property": null
},
"name": null,
"id": null
}],
"/type/reflect/any_value": [{
"link": {
"master_property": null
},
"value": null
}]
}
But that means I'll be missing some information, such as the number of employees because that's given as a "Dated Integer" which, of course, doesn't get automatically expanded and I won't know what I would have to expand in general. My best attempts at expanding all objects by nesting that query once in itself were met with a
"code": 503,
"message": "Backend Error"
In RDF/SPARQL (e.g. DBpedia) I'd just do select ?p ?o where {URI ?p ?o} and select ?s ?p where {?s ?p URI}, am I missing such a simple way to do this in Freebase?
So to summarize, I'm looking for a way to get the information on a Freebase HTML page with as little overhead as possible and without missing anything.
The Topic API was designed specifically for this use case (and is what's used to construct the Freebase HTML page). It takes a filter parameter which can be used to tailor its output to include only parts of the schema which are of interest. What metadata is getting in your way? Why can't you just skip it?
If you'd prefer to use SPARQL, there's an RDF dump available that you could load in your own triple store and query with SPARQL.

Resources