Ember uses Restadapter & Jsonapiadapter for the adapters.
What are the exact differences between the 2 in terms of data formats for request/response ?
Any other things we need to ensure when using any of these 2.
The JSONAPIAdapter conforms to the JSONApi spec
Use RESTAdapter when you have an JSON API that follows a REST endpoint with pluralized object names and has a root node using the name of the object being returned.
Examples below:
Example JSONAPI spec object:
{
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
},
"data": { "type": "people", "id": "7" }
}
},
}],
"included": [{
"type": "people",
"id": "7",
"attributes": {
"name": "Dave",
"twitter": "kiwiupover"
}
}]
}
Example Rest json api object:
{
"posts": {
"id": 5,
"title": "An API that gets bikeshed for months ",
"author": "kiwiupover",
"comments": [1]
},
"comments": [{
"id": 1,
"name": "Dave",
}]
}
Ember Data provides straightforward methods for adapting your DS.adapter to your specific JSON API shape.
There is a third adapter from which the previously mentioned adapters are extended from.
Related
I'm looking solution to query JSON that extracts specific data.
This solution will be embedded inside our Python software (more exactly Python AWS Lambda) and the user should have the ability to specify such query string in GUI.
For example, we have such JSON and I would extract "nazwisko_weterynarza.value" from "files" array if "typ_dokument" is equal to "szczepienie" and "data_szczepienia" is equal to 3434343.
I've tried this using JSON Path but I've failed.
What about embedded Lua/Javascript in Python, but how to create a 'sandbox' to be sure that such a solution will be safe?
{
"files" :
[
{
"meta": {
"updated": 555555,
"attributes": [
{
"typ_dokumentu":
{
"formula_type": "NONE",
"formula": "",
"value_type": "string",
"value": "szczepienie",
"updated": 5345435435345
}
},
{
"data_szczepienia":
{
"formula_type": "NONE",
"formula": "",
"value_type": "int",
"value": 3434343,
"updated": 5345435225345
},
},
{
"nazwisko_weterynarza":
{
"formula_type": "NONE",
"formula": "",
"value_type": "string",
"value": "Nowak",
"updated": 5345435225345
}
}
]
}
},
{
"meta": {
"updated": 555555,
"attributes": [
{
"typ_dokumentu" :
{
"formula_type": "NONE",
"formula": "",
"value_type": "string",
"value": "Certyfikat urodzin",
"updated": 5345435435345
}
},
{
"data_urodzin" :
{
"formula_type": "NONE",
"formula": "",
"value_type": "int",
"value": 8888888,
"updated": 5345435225345
}
}
]
}
}
]
}
The AWS JSON (reference) path queries are not capable enough. Some more advanced JSONPath processors can do it, eg. using Jayway's JsonPath we could run a query like this:
$.files..meta.[?(#.attributes[*].typ_dokumentu.value contains 'szczepienie' && #.attributes[*].data_szczepienia.value contains '3434343')]..nazwisko_weterynarza.value
Reurns ["Nowak"] (You can try it online).
I don't know what you mean by embedded JavaScript/Lua, but in any event, you could create a web service that does the querying for you leveraging an implementation of your choice. Security wouldn't be an issue as long as your API is properly secured.
As a complete newbie to the concept of NoSQL it is giving me a hard time figuring out how to achieve something like a join with multiple linked documents.
I got a Data-Structure for a Congress in a CouchDB which is replicated to a PouchDB on a Client like:
Sessions:
"_id": "1",
"type": "session",
"Title": "Title of Session",
}
Persons:
"_id": "2",
"type": "Person",
"mail": "mail#mail.com",
"Names": {
"FirstName": "Horst",
"LastName": "Muller"
}
"_id": "3",
"type": "Person",
"mail": "mail2#mail.com",
"Names": {
"FirstName": "Mark",
"LastName": "Webber"
}
Talks:
"_id": "4",
"type": "presentation",
"Sessions": [
"1"
],
"PresentationTitle": "Title of Talk",
"files": [
{
"Filename": "presentationfile.pptx",
}
],
"Speakers": [
"2",
"3"
]
What I tried to achieve was an output where the documents are retrieved as if putting the doc of the linked document in the result like
"_id": "4",
"type": "presentation",
"Sessions": [
"Title of Session"
],
"PresentationTitle": "Title of Talk",
"files": [
{
"Filename": "presentationfile.pptx",
}
],
"Speakers": [
{
"FirstName": "Mark",
"LastName": "Webber"
},
{
"FirstName": "Horst",
"LastName": "Muller"
}
]
or something similar without denormalizing the data. I tried with map/reduce, Mango and pouchdb-find, but without luck.
Is that even possible in a NoSQL-World?
There is actually a really neat feature in CouchDB called Linked Documents which allows you to effectively do joins.
The gist is that if you emit an object with a _id property as a value, then the behavior of include_docs=true will fetch the linked document instead of the source document. (the docs I linked to above have a detailed example of this)
Question:
Is there a plain or native javascript way to validate a JSON script against a JSON schema?
I have found lots of libraries on Github, but no native/plain solution. Does EcmaScript not have a specification for this? and do none of the browsers (or nodejs) have a way to validate JSON natively?
Context of Question:
I have a very complex schema that I developed.
It is supposed to work along with a script that requires that the JSON data passed into it to comply with the schema.
Simply, no.
There was something called JSON Schema, which was an Internet Draft which expired in 2013. Internet Drafts are the first stage to producing an Internet Standard. See more about it at the official site, as it seems to potentially still be actively developed, although it is not (to my knowledge) in widespread use.
An example of the schema:
{
"$schema": "http://json-schema.org/schema#",
"title": "Product",
"type": "object",
"required": ["id", "name", "price"],
"properties": {
"id": {
"type": "number",
"description": "Product identifier"
},
"name": {
"type": "string",
"description": "Name of the product"
},
"price": {
"type": "number",
"minimum": 0
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
},
"stock": {
"type": "object",
"properties": {
"warehouse": {
"type": "number"
},
"retail": {
"type": "number"
}
}
}
}
}
will validate this example JSON:
{
"id": 1,
"name": "Foo",
"price": 123,
"tags": [
"Bar",
"Eek"
],
"stock": {
"warehouse": 300,
"retail": 20
}
}
There seems to be at least one pure JS solution now (https://github.com/tdegrunt/jsonschema) available via npm (https://www.npmjs.com/package/jsonschema). I am not a contributor, although I appreciate their work.
I'm new to Ember and from what I understand it has a very specific way it excepts its json api response to look like. Like so:
{
"post": {
"id": 1,
"title": "Node is not omakase",
"comments": [1, 2, 3]
},
"comments": [{
"id": 1,
"body": "But is it _lightweight_ omakase?"
},
{
"id": 2,
"body": "I for one welcome our new omakase overlords"
},
{
"id": 3,
"body": "Put me on the fast track to a delicious dinner"
}]
}
Now the api I've already built has an json response that looks like so:
{
"data": {
"id": 1,
"name": "Pansy Bednar",
"links": [
{
"rel": "self",
"uri": "/pansy-bednar15"
}
],
"players": {
"data": [
{
"id": 2,
"name": "Nicholas O'Reilly",
"position": "cad",
"age": 23,
"value": "640",
"links": [
{
"rel": "self",
"uri": "/team/nicholas-o-reilly71"
}
]
}
]
}
}
}
The api is pretty big and is working fine with the mobile app. So a code re write would be to costly I would just choose another js framework even though I like Ember the best.
So my question is is there any way I can adapt the expected json response in ember. If yes how hard is it? Worth the time or should I just go for Angular or Aurelia.
or am I completely wrong and there is no one expected response to ember?
The thing you can do with ember is to write your very own adapter for this there are already many questions+answers out there:
https://stackoverflow.com/a/17938593/1581725
https://stackoverflow.com/a/24411550/1581725
...
And there is also this blog entry here: http://eviltrout.com/2013/03/23/ember-without-data.html it's about using ember without ember-data.
Found this little gem called normalizePayload - maybe this would also work for your case: https://stackoverflow.com/a/21790093/1581725
I've tried 100 different things, and spend days looking through Google and Stackoverflow, but I can't find a solution to this problem. Everything I call after the body of this API response returns undefined!
The response from Facebook SDK looks like this:
[
{
"body": "[
"data": [
{
"name": "Larry Syid Wright",
"administrator": false,
"id": "xxx"
}, {
"name": "Melissa Long Jackson",
"administrator": false,
"id": "xxx"
}, {
"name": "Charlotte Masson",
"administrator": false,
"id": "xxx"
}
],
"paging": {
"next": "url"
}
]"
},{
"body": "{
"data": [
{
"id": "xxx_xxx",
"message": "In honor of Halloween, how many of you have your own ghost stories? Who believes in ghosts and who doesn't?",
"type": "status",
"created_time": "2014-10-31T20:02:01+0000",
"updated_time": "2014-11-01T02:52:51+0000",
"likes": {
"data": [
{
"id": "xxx",
"name": "Joe HerBatman Owenby Jr."
}
],
}
"paging": {
"cursors":
{
"after": "xxx",
"before": "xxx"
}
}
}
},{
"id": "xxx_xxx",
"from": {
"id": "xxx",
"name": "Jessica Starling"
},
"message": "Watching the "Campaign" and I can't help but notice what a fantastic job they did (Will ferrell and all) with that North Carolina accent! Ya'll know we sound different than other southern states ;)",
"type": "status",
"created_time": "2014-11-01T02:36:21+0000",
"updated_time": "2014-11-01T02:36:21+0000",
"likes": {
"data": [
{
"id": "xxx",
"name": "Scott Williams"n
}
]
}
}
],
"paging": {
"previous": "xxx",
"next": "xxx"
}
}"
}
]
This response is from a batch call. If I call them separately, I can easily iterate through the responses, and get everything from them. When I call them in the batch though, I can't get past "body", and I need to use a batch call.
console.log(response[0].body); will return the object inside the body of the first part of the response, but console.log(response[0].body.data); returns undefined. I just don't get it. This should be simple but it's like there's a lock on the door and I don't have the right key.
I normally have no issue iterating through objects, so I don't need a generalized answer. I need help seeing whatever it is here that I don't see. Why does the console show undefined when I call anything after the body, and what do I need to be doing to get any of these values?
That JSON contains nested JSON. body seems to be a string. Use
var body = JSON.parse(response[0].body);
The values from the body are just strings.which are embedded as json.So firstly you would need to parse them using JSON.parse.
The code would be like
var body = JSON.parse(response[0].body);