How i can delimite entity join level for JSON parse - json

I'm new to web application development with PHP, Symfony (Bundles) and Angular
we develop a web application, works over API rest building on symfony and we make request through http protocol, every response is coded in JSON.
Every JSON correspond to doctrine entity.
We use FosRest to parse entity to JSON, here my config:
fos_rest:
param_fetcher_listener: true
routing_loader:
default_format: json
include_format: json
view:
view_response_listener: 'force'
formats:
xml: false
json: true
templating_formats:
html: false
format_listener:
rules: ~
exception:
codes:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
messages:
'Symfony\Component\Routing\Exception\ResourceNotFoundException': true
allowed_methods_listener: true
access_denied_listener:
json: true
body_listener: true
service:
inflector: app.util.inflector
Doctrine config
doctrine:
dbal:
driver: pdo_sqlsrv #pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
metadata_cache_driver: apc
query_cache_driver: apc
The generated JSON API is correct, here is an example
{
"id": 1,
"username": "admin",
"email": "admin#admin.com",
"enabled": true,
"last_login": "2016-07-04T16:31:07-0500",
"name": "admin",
"first_lastName": "admin",
"second_lastName": "admin",
"id_program": {
"id": 1,
"abbreviation": "LRI",
"nomenclature": "LRI",
"description": "Licenciatura en Relaciones Internacionales",
"consecutive": "1",
"cve_dgp": "3453454355",
"first_period": "2016-06-09T10:30:00-0500",
"campus": "HARVARD",
"mod": "Escolarizado",
"number": "863676476",
"register_date": "2016-06-09T10:30:00-0500",
"ref_number": "0976555",
"require_pay": true,
"colour_rgb": "253545454",
"last_modification": "2016-06-17T12:51:28-0500",
"id_level": {
"id": 1,
"cve": "LIC",
"description": "Licenciatura",
"last_modification": "2016-06-11T10:58:44-0500",
"id_status": {
"id": 1008,
"value": "Inactivo",
"active": true,
"id_cat": {
"id": 1004,
"valur": "CatalogosEstatus",
"active": true
}
}
},
"id_front_credenctial_file": {
"id": 193,
"name": "FRENTE-LRI.jpg",
"extention": "jpeg",
"mime_type": "image/jpeg",
"md5": "e5643b567aa6e134ae1abcc23fdb545e",
"is_perfil": false
},
"id_back_file_credential": {
"id": 194,
"name": "REVES-LRI.jpg",
"extention": "jpeg",
"mime_type": "image/jpeg",
"md5": "d490d35f448d3fcb849cc708f7291bc5",
"is_perfil": false
},
"id_status": {
"id": 1007,
"value": "Activo",
"active": true,
"id_cat": {
"id": 1004,
"value": "CatalogosEstatus",
"active": true
}
}
},
"ldap": true,
"rolesdb": [{
"id": 8,
"id_rol": {
"id": 9,
"name": "ADMINISTRADOR",
"description": "Rol Administrador",
"active": true
}
}, {
"id": 9,
"id_rol": {
"id": 10,
"name": "SHOWCASE",
"description": "Rol showcase",
"active": true
}
}, {
"id": 10,
"id_rol": {
"id": 11,
"name": "DESARROLLADOR",
"description": "Rol desarrollador",
"active": true
}
}]}
The problem I have is when an entity has multiple relationships , as you can see in the JSON example , when parsing the entity being added all your relationships (each key that starts with id_ is a mapped relationship with Doctrine) and as our BD has grown considerably , there are times when a JSON has too much information that is hard to read from Angular side .
Is there a way to define (optimize ) until the parser level (JSON ) relations is made in an entity ?
or not include each join of the entity?
regards!!

with #MaxDepth you can steer how deep you want to serialize your hierarchy and with #Groups you can manage which fields should be serialized.
Both can be set in your Serialization Context and are explained in more detail in the FOS Rest Documentation
By that you should be able to achieve what you're looking for.

Related

parse json response using ansible

I need to parse the json response from an API request using ansible and then take action on items returned in the response. Here is part of my ansible playbook -
```- name: Invoke Import API
uri:
url: "{{ atl_bitbucket_dataset_url }}/rest/api/1.0/migration/imports"
user: admin
password: "{{ atl_bitbucket_admin_password }}"
method: POST
follow_redirects: yes
force_basic_auth: yes
creates: "{{ atl_product_home_shared }}/data/migration/import/lock.file"
body: "{ \"archivePath\": \"{{ atl_bitbucket_dataset_url | basename }}\" }"
body_format: json
return_content: yes
register: response
until: response.status == 200
retries: 6
delay: 15
failed_when: response.response.json.state != 'INITIALISING'
- name: get status of import
debug: var=response```
and here is the json response i got on a previous run
```TASK [bitbucket_dataset_restore : get status of import] ************************
ok: [localhost] => {
"response": {
"attempts": 1,
"cache_control": "no-cache, no-transform",
"changed": false,
"connection": "close",
"content": "{\"id\":1,\"initiator\":{\"name\":\"admin\",\"emailAddress\":\"admin#yourcompany.com\",\"id\":1,\"displayName\":\"AdminIstrator\",\"active\":true,\"slug\":\"admin\",\"type\":\"NORMAL\",\"links\":{\"self\":[{\"href\":\"http://bbdc-test-loadbala-t6vnlr2363vl-1404860112.us-west-2.elb.amazonaws.com/users/admin\"}]}},\"nodeId\":\"e72aa995-5016-4b2c-80e8-edba5eda3ab4\",\"progress\":{\"percentage\":0},\"startDate\":1574803309090,\"state\":\"INITIALISING\",\"type\":\"com.atlassian.bitbucket.migration.import\",\"updateDate\":1574803309090}",
"content_type": "application/json;charset=UTF-8",
"cookies": {},
"cookies_string": "",
"date": "Tue, 26 Nov 2019 21:21:49 GMT",
"elapsed": 1,
"failed": false,
"failed_when_result": False,
"json": {
"id": 1,
"initiator": {
"active": True,
"displayName": "AdminIstrator",
"emailAddress": "admin#yourcompany.com",
"id": 1,
"links": {
"self": [
{
"href": "http://bbdc-test-loadbala-t6vnlr2363vl-1404860112.us-west-2.elb.amazonaws.com/users/admin"
}
]
},
"name": "admin",
"slug": "admin",
"type": "NORMAL"
},
"nodeId": "e72aa995-5016-4b2c-80e8-edba5eda3ab4",
"progress": {
"percentage": 0
},
"startDate": 1574803309090,
"state": "INITIALISING",
"type": "com.atlassian.bitbucket.migration.import",
"updateDate": 1574803309090
},
"msg": "OK (unknown bytes)",
"redirected": False,
"status": 200,
"transfer_encoding": "chunked",
"url": "http://localhost:7990/rest/api/1.0/migration/imports",
"vary": "accept-encoding,x-auserid,cookie,x-ausername,accept-encoding",
"warnings": [
"The value True (type bool) in a string field was converted to u'True' (type string). If this does not look like what you expect, quote the entire value to ensure it does not change."
],
"x_arequestid": "#XXAH7Kx1281x1x0",
"x_asen": "SEN-500",
"x_auserid": "1",
"x_ausername": "admin",
"x_content_type_options": "nosniff"
}
}```
I want to retrieve the value "state": "INITIALISING" and the "id": 1. I have tried various methods response.response.json.state or response['response']['json']['state']. And it has not worked. Can anyone help me on how i can retrieve those values so i can use in other tasks further down?
I was able to fix this by adding is defined. So my code looks like this -
failed_when: output is defined and output.json is defined and output.json.state != 'INITIALISING'

What is the best method to seeding a Node / MongoDB application?

So, I'm new to the MEAN stack, and I've hit a wall trying to seed MongoDB. I'm using Mongoose to communicate with the database, and there's a bunch of documentation suggesting I should be able to seed using populated JSON files.
What I've tried:
node-mongo-seed; Pretty straight forward, but consistently throws errors on the end of arrays. (Perhaps the missing bson module is at fault?)
{ [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' }
js-bson: Failed to load c++ bson extension, using pure JS version
Seeding files from directory /Users/Antwisted/code/wdi/MEAN/seeds
----------------------
Seeding collection locations
err = [SyntaxError: /Users/Antwisted/code/wdi/MEAN/seeds/locations.json: Unexpected token {]
mongoose-seed; Also pretty straight forward, basically puts the JSON objects into a variable before exporting to the database. Promising, but... more errors...
Successfully initialized mongoose-seed
[ 'app/models/locationsModel.js' ]
Locations collection cleared
Error creating document [0] of Location model
Error: Location validation failed
Error creating document [1] of Location model
Error: Location validation failed
Error creating document [2] of Location model
Error: Location validation failed...
So, my thoughts were that it was probably a syntax error within the JSON structure, but playing around with that has not yielded any real solutions (or maybe I'm missing it?). Sample of my JSON:
{
{
"header": "Dan's Place",
"rating": 3,
"address": "125 High Street, New York, 10001",
"cord1": -73.0812,
"cord2": 40.8732,
"attributes": ["Hot drinks", "Food", "Premium wifi"],
"hours": [
{
"days": "Monday - Friday",
"hours": "7:00am - 7:00pm",
"closed": false
},
{
"days": "Saturday",
"hours": "8:00am - 5:00pm",
"closed": false
},
{
"days": "Sunday",
"closed": true
}
],
"reviews": [
{
"rating": 4,
"id": ObjectId(),
"author": "Philly B.",
"timestamp": "new Date('Feb 3, 2016')",
"body": "It was fine, but coffee was a bit dull. Nice atmosphere."
},
{
"rating": 3,
"id": ObjectId(),
"author": "Tom B.",
"timestamp": "new Date('Feb 23, 2016')",
"body": "I asked for her number. She said no."
}
]
},
{
"header": "Jared's Jive",
"rating": 5,
"address": "747 Fly Court, New York, 10001",
"cord1": -73.0812,
"cord2": 40.8732,
"attributes": ["Live Music", "Rooftop Bar", "2 Floors"],
"hours": [
{
"days": "Monday - Friday",
"hours": "7:00am - 7:00pm",
"closed": false
},
{
"days": "Saturday",
"hours": "8:00am - 5:00pm",
"closed": false
},
{
"days": "Sunday",
"closed": true
}
],
"reviews": [
{
"rating": 5,
"id": ObjectId(),
"author": "Jacob G.",
"timestamp": "new Date('Feb 3, 2016')",
"body": "Whoa! The music here is wicked good. Definitely going again."
},
{
"rating": 4,
"id": ObjectId(),
"author": "Tom B.",
"timestamp": "new Date('Feb 23, 2016')",
"body": "I asked to play her a tune. She said no."
}
]
}
}
Additionally, I'm not entirely sure how to specify subdocuments within the JSON (assuming I can get the seeding process to work correctly in the first place).
Here's my model:
var mongoose = require('mongoose');
var subHoursSchema = new mongoose.Schema({
days: {type: String, required: true},
opening: String,
closing: String,
closed: {type: Boolean, required: true}
});
var subReviewsSchema = new mongoose.Schema({
rating: {type: Number, required: true, min: 0, max: 5},
author: String,
timestamp: {type: Date, "default": Date.now},
body: String
});
var locationSchema = new mongoose.Schema({
name: {type: String, required: true},
address: String,
rating: {type: Number, "default": 0, min: 0, max: 5},
attributes: [String],
coordinates: {type: [Number], index: '2dsphere'},
openHours: [subHoursSchema],
reviews: [subReviewsSchema]
});
mongoose.model('Location', locationSchema);
Any insight on how to navigate these issues would be greatly appreciated. Thanks!
You can populate MongoDB in the CLI using mongoimport
It will load a JSON file into a specified MongoDB Instance & Collection, all you need is for a mongod instance to be running before executing.
Here is a walkthrough of using mongoimport.
You JSON is not flowing your schema.
Fix your JSON to this:
{
{
"name": "Dan's Place",
"rating": 3,
"address": "125 High Street, New York, 10001",
"coordinates": [-73.0812, 40.8732],
"attributes": ["Hot drinks", "Food", "Premium wifi"],
"openHours": [
{
"days": "Monday - Friday",
"opening": "7:00am",
"closing": "7:00pm",
"closed": false
},
{
"days": "Saturday",
"opening": "8:00am",
"closing": "5:00pm",
"closed": false
},
{
"days": "Sunday",
"closed": true
}
],
"reviews": [
{
"rating": 4,
"author": "Philly B.",
"timestamp": "new Date('Feb 3, 2016')",
"body": "It was fine, but coffee was a bit dull. Nice atmosphere."
},
{
"rating": 3,
"author": "Tom B.",
"timestamp": "new Date('Feb 23, 2016')",
"body": "I asked for her number. She said no."
}
]
},
{
"name": "Jared's Jive",
"rating": 5,
"address": "747 Fly Court, New York, 10001",
"coordinates": [-73.0812, 40.8732],
"attributes": ["Live Music", "Rooftop Bar", "2 Floors"],
"openHours": [
{
"days": "Monday - Friday",
"opening": "7:00am",
"closing": "7:00pm",
"closed": false
},
{
"days": "Saturday",
"opening": "8:00am",
"closing": "5:00pm",
"closed": false
},
{
"days": "Sunday",
"closed": true
}
],
"reviews": [
{
"rating": 5,
"author": "Jacob G.",
"timestamp": "new Date('Feb 3, 2016')",
"body": "Whoa! The music here is wicked good. Definitely going again."
},
{
"rating": 4,
"author": "Tom B.",
"timestamp": "new Date('Feb 23, 2016')",
"body": "I asked to play her a tune. She said no."
}
]
}
}
You can use mongoose-data-seed to write your own seed script that interacting your mongoose models with:
https://github.com/sharvit/mongoose-data-seed
I solved this issue on a project by dumping the relevant data to an extended JSON array formatted file using mongoexport --jsonArray, then importing this back into POJO format inside the Node application using the EJSON package. I then just use Mongoose to insert the resulting JS array back into the database using the correct collection model you've created using Mongoose.
The necessary JSON data files to seed the application for a first-run are checked into the application repository.
Here's a quick sample you may be able to adapt to your purposes:
// ...
// 'Items' is the Mongoose collection model.
const itemResult = await Items.find({}).exec();
if(itemResult.length === 0) {
const itemsSeedDataRaw = fs.readFileSync(`${__dirname}/data/items.json`, 'utf8');
const itemsSeedData = EJSON.parse(itemsSeedDataRaw);
await Items.insertMany(itemsSeedData);
}
// ...
I would also recommend looking into mongo-seeding. There is both a JS library version and a CLI version. The motivation for this library is described here.

Serialize the response from backend to store ember store

My response from backend is not in form which ember store. I am not able to serialize the response.
response.json
[{
"pk": 127,
"url": "http://example.com/api/galleries/127/",
"gallery_name": "Faces",
"thumbnail_url": "https://example.cloud.net/galleryThumbs/2656a05c-4ec7-3eea-8c5e-d8019454d443.jpg",
"time": "1 month ago",
"description": "Created by user",
"is_following": true,
"feedPhotos": [{
"pk": 624,
"url": "http://example.com/api/photos/624/",
"profilePic": "https://example.cloud.net/userDPs/50906ce2-394d-39c8-9261-8cf78e3611c2.jpg",
"userName": "Nabeela",
"userKarma": 915,
"caption": "Old woman spinning her 'chhos-khor' ...a rotation of which is equivalent to the recitation of a mantra.",
"numComments": 0,
"owner": "http://example.com/api/users/44/",
"time": "1 month ago",
"photo_url": "https://example.cloud.net/photos/9cbd6423-3bc5-36e0-b8b4-d725efb3249a.jpg",
"comments_url": "http://example.com/api/photos/624/comments/",
"numFives": 4,
"fivers_url": "http://example.com/api/photogalleries/1362/fivers/",
"fivers_pk": 1362,
"fullphoto_url": "http://example.com/api/photogalleries/1362/photo/",
"fullphoto_pk": 1362,
"is_fived": true,
"hiFiveKarma": 1,
"owner_pk": 44,
"userFirstName": "Nabeela",
"is_bookmarked": false
}, {
"pk": 574,
"url": "http://example.com/api/photos/574/",
"profilePic": "https://example.cloud.net/userDPs/b6f69e4e-980d-3cc3-8b3e-3eb1a7f21350.jpg",
"userName": "Rohini",
"userKarma": 194,
"caption": "Life # Myanmar!",
"numComments": 0,
"owner": "http://example.com/api/users/45/",
"time": "2 months ago",
"photo_url": "https://example.cloud.net/photos/eeae72d5-d6af-391e-a218-b442c0c7e34e.jpg",
"comments_url": "http://example.com/api/photos/574/comments/",
"numFives": 2,
"fivers_url": "http://example.com/api/photogalleries/1303/fivers/",
"fivers_pk": 1303,
"fullphoto_url": "http://example.com/api/photogalleries/1303/photo/",
"fullphoto_pk": 1303,
"is_fived": false,
"hiFiveKarma": 0,
"owner_pk": 45,
"userFirstName": "Rohini",
"is_bookmarked": false
}
]
}, {
"pk": 65,
"url": "http://example.com/api/galleries/65/",
"gallery_name": "Royal",
"thumbnail_url": "https://example.cloud.net/galleryThumbs/d8a900af-1f1d-3977-8cc8-b8bb36e32be5.jpg",
"time": "2 months ago",
"description": "This is a gallery about Royal",
"is_following": false,
"feedPhotos": [{
"pk": 347,
"url": "http://example.com/api/photos/347/",
"profilePic": "https://example.cloud.net/userDPs/50906ce2-394d-39c8-9261-8cf78e3611c2.jpg",
"userName": "Nabeela",
"userKarma": 915,
"caption": "I cannot forget the name of this palace - Moti Mahal (translation: Pearl Palace). Indescribably beautiful, ainnit! at Mehrangarh fort, Jodhp",
"numComments": 0,
"owner": "http://example.com/api/users/44/",
"time": "2 months ago",
"photo_url": "https://example.cloud.net/photos/958ed406-708e-3f01-a2f4-9467cd709fdd.jpg",
"comments_url": "http://example.com/api/photos/347/comments/",
"numFives": 4,
"fivers_url": "http://example.com/api/photogalleries/759/fivers/",
"fivers_pk": 759,
"fullphoto_url": "http://example.com/api/photogalleries/759/photo/",
"fullphoto_pk": 759,
"is_fived": false,
"hiFiveKarma": 0,
"owner_pk": 44,
"userFirstName": "Nabeela",
"is_bookmarked": false
}, {
"pk": 593,
"url": "http://example.com/api/photos/593/",
"profilePic": "https://example.cloud.net/userDPs/95ac6974-f7df-338c-ab84-99fa1df7514c.jpg",
"userName": "Vikanshu",
"userKarma": 932,
"caption": "Marvelous architecture!! in Florence, Italy",
"numComments": 0,
"owner": "http://example.com/api/users/48/",
"time": "1 month ago",
"photo_url": "https://example.cloud.net/photos/7a86eb37-6c68-3d6c-b6cf-2e3b74d330dd.jpg",
"comments_url": "http://example.com/api/photos/593/comments/",
"numFives": 4,
"fivers_url": "http://example.com/api/photogalleries/1363/fivers/",
"fivers_pk": 1363,
"fullphoto_url": "http://example.com/api/photogalleries/1363/photo/",
"fullphoto_pk": 1363,
"is_fived": false,
"hiFiveKarma": 0,
"owner_pk": 48,
"userFirstName": "Vikanshu",
"is_bookmarked": false
}]
}]
How do I serialize this using JSONPISerailizer or any other serializer in ember-cli so that it gets stored in ember store
Reference jsonapi.org
++++Top Level:
Root:
A JSON object must be root of every JSON API request response.
A document must contain at least one top-level members:
1. data: documents "primary data"
2. errors: an array of error objects (id,status,code,title....)
3. meta: a meta object that contains non-standard meta-information (copyright,author...)
member data and errors must not co-exist together.
"data"{}
+++++Resource Objects
1. A resource object MUST contain atleast following top-level member
*id
*type
```
//structure-1
//for galleries
{
"data": {
"type": "galleries",
"id": "1"
}
}
//for photos
{
"data": {
"type": "photos",
"id": "1"
}
}
```
In addition, a resource object may contain any of these top-level members
*attributes
*relationship
*links
*meta
//added attributes first
```
//structure-2
//for galleries
{
"data": {
"type": "galleries",
"id": "1",
"attributes": {
"galleryName": "Faces"
"thumbnailUrl:"https://example.cloud.net/galleryThumbs/2656a05c-4ec7.jpg",
"description": "Created by user",
}
}
}
//for photos
{
"data": {
"type": "photos",
"id": "1",
"attributes":{
userName: "Nabeela",
userKarma: 915
}
}
}
```
//Adding relationship
Relationship object must contain atleast one of the following
*links (containing atleast one of "self" or "related" resource link
*data
*meta
//link in relationship (minimum one required from link,data,meta).
//
```
//structure-3
//for galleries
{
"data":[{ //Array(square bracket as adding relationship one more item to data
"type": "galleries",
"id": "1",
"attributes": {
"galleryName": "Faces"
"thumbnailUrl:"https://example.cloud.net/galleryThumbs/2656a05c-4ec7.jpg",
"description": "Created by user",
},
"relationships":{
"links":{
"self": "https://example.cloud.net/photos/9cbd6423.jpg //"photo_url" in your payload
},
}]
}
}
```
//data in relationship
```
//structure-4
//for galleries
{
"data": [{
"type": "galleries",
"id": "1",
"attributes": {
"galleryName": "Faces"
"thumbnailUrl:"https://example.cloud.net/galleryThumbs/2656a05c-4ec7.jpg",
"description": "Created by user",
},
"relationships":{ //This has all your photo stuff
"links":{
"self": "https://example.cloud.net/photos/9cbd6423.jpg //"photo_url" in your payload
},
"data": { //picked it up from structure-1
"type": "photos",
"id": "77"
}
}]
}
}
```
//Adding related resource "included"
```
//for galleries
{
"data": [{
"type": "galleries",
"id": "1",
"attributes": {
"galleryName": "Faces"
"thumbnailUrl:"https://example.cloud.net/galleryThumbs/2656a05c-4ec7.jpg",
"description": "Created by user",
},
"relationships":{ //This has all your photo stuff
"links":{
"self": "https://example.cloud.net/photos/9cbd6423.jpg //"photo_url" in your payload
},
"data": { //picked it up from structure-1
"type": "photos",
"id": "77"
}
}],
"included":[{
"type": "photos",
"id": "77",
"attributes":{
userName: "Nabeela",
userKarma: 915
},
{
"type": "photos",
"id": "78",
"attributes":{
userName: "Nabeela",
userKarma: 915
}
}]
}
}
```
For collections. I am not confident but try this
Now for collection of galleries.
//for galleries
{
"data": [{
"type": "galleries",
"id": "1",
"attributes": {
"galleryName": "Faces"
"thumbnailUrl:"https://example.cloud.net/galleryThumbs/2656a05c-4ec7.jpg",
"description": "Created by user",
},
"relationships":{ //This has all your photo stuff
"links":{
"self": "https://example.cloud.net/photos/9cbd6423.jpg //"photo_url" in your payload
},
"data": { //picked it up from structure-1
"type": "photos",
"id": "77"
}
},{
"type": "galleries",
"id": "2",
"attributes": {
"galleryName": "Faces"
"thumbnailUrl:"https://example.cloud.net/galleryThumbs/2656a05c-4ec7.jpg",
"description": "Created by user",
},
"relationships":{ //This has all your photo stuff
"links":{
"self": "https://example.cloud.net/photos/9cbd6423.jpg //"photo_url" in your payload
},
"data": { //picked it up from structure-1
"type": "photos",
"id": "79"
}
}],
"included":[{
"type": "photos",
"id": "77",
"attributes":{
userName: "Nabeela",
userKarma: 915
},{
"type": "photos",
"id": "78",
"attributes":{
userName: "Nabeela",
userKarma: 915
},{
"type": "photos",
"id": "79",
"attributes":{
userName: "Nabeela",
userKarma: 915
}
}]
}
}
============Implementation part =================================
JSONSerializer normalization process follows these steps
*normalizeResponse : entry method.
*normalizeCreateRecordResponse : a normalizeResponse for specific operation.
*normalizeSingleResponse|normalizeArrayResponse:
- for methods like createRecord. we expect a single record back.
- for methods like findAll we expect multiple records back.
+normalize =
normalizeArray iterates and calls normalize for each of it's records
normalizeSingle call its once.
+extractID | extractAttributes | extractRelationships
= normalize delegates to these method to turn record payload into jsonAPI format
Starting with normalizeResponse method. If you open and see normalizeResponse method
in json-serializer
link normalizeResponse: https://github.com/emberjs/data/blob/v2.2.1/packages/ember-
data/lib/serializers/json-serializer.js#L192
you with find a switch case switch(requestType). If requestType if
"findRecord" then "normalizeFindRecordResponse" is called
"queryRecord" then "normalizeQueryRecordResponse" is called
"findAll" then "normalizeFindAllResponse" is called
...so on and so forth.
if you notice the parameter passed to all the methods are same as that of normalize
(...arguments) :)
**Lets start for findAll
i.e normalizeResponse -> normalizeFindAllResponse -> normalizeArrayResponse
as normalizeFindAllResponse method has only one line that call
normalizeArrayResponse.
normalizeFindAllResponse
normalizeResponse -> normalizeFindAllResponse -> normalizeArrayResponse ->
_normalizeResponse{ extractMeta,normalize }
extractMeta [extract meta information like pagination and stuff ]
if single: normalize []
example of normalize method in emberjs docs
```
import DS from 'ember-data';
export default DS.JSONSerializer.extend({
normalize: function(typeClass, hash) {
var fields = Ember.get(typeClass, 'fields');
fields.forEach(function(field) {
var payloadField = Ember.String.underscore(field);
if (field === payloadField) { return; }
hash[field] = hash[payloadField];
delete hash[payloadField];
});
return this._super.apply(this, arguments);
}
});
```
"normalizeArrayResponse calls `return this._normalizeResponse
(store,primaryModelClass,payload,id,requestType,false).
so isSingle is false for _normalizeResponse method. so we will have to push all the
related records of included array
in our case the photos which is done by below snippet from "_normalizeRespose"
method.
_normalizeResponse
```
else{
documentHash.data = payload.map((item) => {
let { data, included } = this.normalize(primaryModelClass,item);
if(included){
documentHash.included.push(...included);
}
return data;
});
return documentHash;
}
```
Things are still unclear in the context of our JSON reponse from server
but atleast we know the flow now.
Lets try to apply it for findAll ( as per the flow above).
run "ember g serializer application" //assuming you are using ember-cli and you
intend to make this serializer generic for application.
As of now I have no information how and when normalizeResponse is called. :(
I just scanned through and guess on recieving data from server the store calls
normalizeResponseHelpers which in turn calls normalizeResponse.
In any case "normalizeResponse" is going to send payload and other necessar
information to normalizeFindAllResponse(...arguments) which in turn will call
normalizeArrayResponse(...arguments) which in turn will call "_normalizeRespone".
Here is where we need to take action
for extraMeta and normalize.
+extraMeta
I am not sure if there is any meta information in you json response.
in case there is you can refer to the example mentioned in docs
extractMeta
So I guess you can directly user the normalize method from example in your application ;).
please try and check. Since i am learning ember myself I cannot guarantee it will work but it should. the lonngggg explation is my thought while i was learning the problem/solution
//app/serializers/application.js
+normalize
```
import DS from 'ember-data';
export default DS.JSONSerializer.extend({
normalize: function(typeClass, hash) {
var fields = Ember.get(typeClass, 'fields');
fields.forEach(function(field) {
var payloadField = Ember.String.underscore(field);
if (field === payloadField) { return; }
hash[field] = hash[payloadField];
delete hash[payloadField];
});
return this._super.apply(this, arguments);
}
});
```
The primary key in the JSON from server is pk. You will have to mention that too
http://emberjs.com/api/data/classes/DS.JSONSerializer.html#property_primaryKey
app/serializers/application.js
import DS from 'ember-data';
export default DS.JSONSerializer.extend({
primaryKey: 'pk'
});

deserializing JSon with different Parent node names

I'm trying to deserializing a json response from Gitblit using c# and i'm getting it back in the following format
{
"https://localhost/git/libraries/xmlapache.git": {
"name": "libraries/xmlapache.git",
"description": "apache xmlrpc client and server",
"owner": "admin",
"lastChange": "2010-01-28T22:12:06Z",
"hasCommits": true,
"showRemoteBranches": false,
"useTickets": false,
"useDocs": false,
"accessRestriction": "VIEW",
"isFrozen": false,
"showReadme": false,
"federationStrategy": "FEDERATE_THIS",
"federationSets": [
"libraries"
],
"isFederated": false,
"skipSizeCalculation": false,
"skipSummaryMetrics": false,
"size": "102 KB"
},
"https://localhost/git/libraries/smack.git": {
"name": "libraries/smack.git",
"description": "smack xmpp client",
"owner": "admin",
"lastChange": "2009-01-28T18:38:14Z",
"hasCommits": true,
"showRemoteBranches": false,
"useTickets": false,
"useDocs": false,
"accessRestriction": "VIEW",
"isFrozen": false,
"showReadme": false,
"federationStrategy": "FEDERATE_THIS",
"federationSets": [],
"isFederated": false,
"skipSizeCalculation": false,
"skipSummaryMetrics": false,
"size": "4.8 MB"
}
}
and i'm having trouble creating the DataContract due to the parent nodes being named from the pull url and could be anything rather than a standardised name. i only need to deserialize "name":,"description":, "owner":, "lastChange" from underneath them.
so is there a way i can deserialize each dataset while ignoring the parent node all together?

How to map json data coming from an REST-API to ember.js model?

I'm new to EmberJS and try to connect my EmberJS-frontend to an API via the RESTAdapter. I'm using ember-1.0.0-rc.6 and ember-data-latest (Version: v0.13-59-ge999edb).
From the API I get the following JSON code:
{
"page": 1,
"limit": 5,
"total": 4,
"results": [
{
"id": 1,
"title": "something3",
"description": "asd3"
},
{
"id": 2,
"title": "something3",
"description": "asd3"
},
{
"id": 3,
"title": "something2",
"description": "asd2"
},
{
"id": 4,
"title": "something",
"description": "asd"
}
]
}
My RESTAdapter looks like the following:
App.Store = DS.Store.extend({
revision: 13,
adapter: DS.RESTAdapter.extend({
url:"http://localhost/api/app_dev.php"
})
});
My model looks like this at the moment:
App.Modelname = DS.Model.extend({
title: DS.attr('string')
});
With this I get the following error in the browser console:
Assertion failed: Your server returned a hash with the key page but you have no mapping for it
MY question is how to connect the json output from the api to my ember model without changing the api on the server?
Thanks in advance for your support !
If your model is like this:
App.Modelname = DS.Model.extend({
title: DS.attr('string')
});
Then the RESTAdapter expects a JSON format like this:
{
"modelNames": [
{
"id": 1,
"title": "something3",
"description": "asd3"
},
{
"id": 2,
"title": "something3",
"description": "asd3"
},
{
"id": 3,
"title": "something2",
"description": "asd2"
},
{
"id": 4,
"title": "something",
"description": "asd"
}
]
}
You can of course tweak some of this behavior by configuring some aspects of the JSONSerializer and the RESTAdapter, IMO to have full control I would suggest to create your own adapter by overriding the vital functions like find, findAll etc. and go with plain $.ajax(...). Otherwise you would need to make your backend obey the JSON format ember-data expects.
Hope it helps, at least to take a decision.