Which schema is better in web service API design - php

Recently, our team is going to develop mobile(iphone, android platforms) applications for our existing website, let user can use the application to more easy to read our content via the application.
But our team have different views in JSON schema of the API return, below are the sample response.
Schema type 1:
{
"success": 1,
"response": {
"threads": [
{
"thread_id": 9999,
"title": "Topic haha",
"content": "blah blah blah",
"category": {
"category_id": 100,
"category_name": "Chat Room",
"category_permalink": "http://sample.com/category/100"
},
"user": {
"user_id": 1,
"name": "Hello World",
"email": "helloworld#hello.com",
"user_permalink": "http://sample.com/user/Hello_World"
},
"post_ts": "2012-12-01 18:16:00T0800"
},
{
"thread_id": 9998,
"title": "asdasdsad ",
"content": "dsfdsfdsfds dsfdsf ds",
"category": {
"category_id": 101,
"category_name": "Chat Room 2",
"category_permalink": "http://sample.com/category/101"
},
"user": {
"user_id": 2,
"name": "Hello baby",
"email": "hellobaby#hello.com",
"user_permalink": "http://sample.com/user/2"
},
"post_ts": "2012-12-01 18:15:00T0800"
}
]
}
}
Schema type 2:
{
"success": 1,
"response": {
"threads": [
{
"thread_id": 9999,
"title": "Topic haha",
"content": "blah blah blah",
"category": 100,
"user": 1,
"post_ts": "2012-12-01 18:16:00T0800"
},
{
"thread_id": 9998,
"title": "asdasdsad ",
"content": "dsfdsfdsfds dsfdsf ds",
"category": 101,
"user": 2,
"post_ts": "2012-12-01 18:15:00T0800"
}
],
"category": [
{
"category_id": 100,
"category_name": "Chat Room",
"category_permalink": "http://sample.com/category/100"
},
{
"category_id": 101,
"category_name": "Chat Room 2",
"category_permalink": "http://sample.com/category/101"
}
],
"user": [
{
"user_id": 1,
"name": "Hello World",
"email": "helloworld#hello.com",
"user_permalink": "http://sample.com/user/Hello_World"
},
{
"user_id": 2,
"name": "Hello baby",
"email": "hellobaby#hello.com",
"user_permalink": "http://sample.com/user/Hello_baby"
}
]
}
}
Some Developers claim that if using schema type 2,
can reduce data size if the category & user entities comes too much duplicated. it does really reduce at least 20~40% size of response plain text.
once if the data size come less, in parsing it to JSON object, the memory get less
categoey & user can be store in hash-map, easy to reuse
reduce the overhead on retrieving data
I have no idea on it if schema type 2 does really enhanced. Because I read so many API documentation, never seen this type of schema design. For me, it looks like a relational database. So I have few questions, because I have no experience on designing a web services API.
Does it against API design principle (Easy to read, Easy to use) ?
Does it really get faster and get less memory resource on parsing on IOS / Android platform?
Does it can reduce the overhead between client & server?
Thanks you.

When I do such an application for android, I parse JSON just one and put it in database. Later I'm using ContentProvider to access it. In Your case You could use 2nd schema but without user, category part. Use lazy loading instead but it will be good solution just in case categories and users repeat often.

Related

How set JSON data to match like Strapi API JSON

Does anybody have experience using Strapi.io and combine it with Codeigniter.
I have difficult when i need to make JSON in my controller same like strapi needed.
My JSON look like below
string(102) "{"id":"3","Name":"Paket Anak-anak","Description":"Loren ipsum","Price":"11000","Image":"Capture3.PNG"}"
Whilist what Strapi need to POST request
kind of like below
{
"data": [
{
"id": 2,
"attributes": {
"Name": "Paket Medium",
"Description": "Loren ipsum qqiqqfqqv",
"Price": 120000,
"Image": null,
"createdAt": "2022-09-17T05:44:18.713Z",
"updatedAt": "2022-09-17T05:44:20.723Z",
"publishedAt": "2022-09-17T05:44:20.720Z"
}
}
],
}

How to update document in one collection when a document of other collection is changed or updated in MongoDB

I've created two collections "Users" and "Posts".
Users document structure is as follows:
{
"_id": {
"$oid": "54dde0e32a2a999c0f00002a"
},
"first_name": "Vamsi",
"last_name": "Krishna",
"email": "vamshi#test.com",
"password": "5f4dcc3b5aa765d61d8327deb882cf99",
"date_of_birth": "1999-01-05",
"gender": "male",
"status": "Active",
"date_created": "2015-02-13 12:32:50"
}
While posts document structure is:
{
"_id": {
"$oid": "54e1a2892a2a99d00500002b"
},
"post_description": "Test post 1",
"posted_by": {
"id": "54dde0e32a2a999c0f00002a",
"first_name": "Vamsi",
"last_name": "Krishna",
"gender": "male"
},
"posted_on": "2015-02-16 08:55:53",
"comments": [],
"likes": {
"count": 0,
"liked_by": []
}
}
My query is that when user updates his information it should reflect everywhere like posted by, commented by and liked by. How can I achieve that?
I'm using PHP.
Thanks!!
Mongodb does not have a notion similar to sql on update cascade, so you have to do this in your application (whenever you update user information, update all other documents that relate to this user in other collections).
As you might have guessed this is super inefficient when there are a lot of such documents, which means that your schema is bad. Just have a userID in your document and this will link to your user's collection.

JSON Schema Requirement Enforcement

So this is my first time using JSON Schema and I have a fairly basic question about requirements.
My top level schema is as follows:
schema.json:
{
"id": "http://localhost/srv/schemas/schema.json",
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"event": { "$ref": "events_schema.json#" },
"building": { "$ref": "buildings_schema.json#" }
},
"required": [ "event" ],
"additionalProperties": false
}
I have two other schema definition files (events_schema.json and buildings_schema.json) that have object field definitions in them. The one of particular interest is buildings_schema.json.
buildings_schema.json:
{
"id": "http://localhost/srv/schemas/buildings_schema.json",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "buildings table validation definition",
"type": "object",
"properties": {
"BuildingID": {
"type": "integer",
"minimum": 1
},
"BuildingDescription": {
"type": "string",
"maxLength": 255
}
},
"required": [ "BuildingID" ],
"additionalProperties": false
}
I am using this file to test my validation:
test.json:
{
"event": {
"EventID": 1,
"EventDescription": "Some description",
"EventTitle": "Test title",
"EventStatus": 2,
"EventPriority": 1,
"Date": "2007-05-05 12:13:45"
},
"building": {
"BuildingID": 1,
}
}
Which passes validation fine. But when I use the following:
test2.json
{
"event": {
"EventID": 1,
"EventDescription": "Some description",
"EventTitle": "Test title",
"EventStatus": 2,
"EventPriority": 1,
"Date": "2007-05-05 12:13:45"
}
}
I get the error: [building] the property BuildingID is required
Inside my buildings_schema.json file I have the line "required": [ "BuildingID" ] which is what causes the error. It appears that the schema.json is traversing down the property definitions and enforcing all the requirements. This is counter intuitive and I would like it to ONLY enforce a requirement if it's parent property is enforced.
I have a few ways around this that involve arrays and fundamentally changing the structure of the JSON, but that kind of defeats the purpose of my attempts at validating existing JSON. I have read over the documentation (/sigh) and have not found anything relating to this issue. Is there a some simple requirement inheritance setting I am missing?
I am using the Json-Schema for PHP implementation from here: https://github.com/justinrainbow/json-schema
After messing with different validators, it appears to be an issue with the validator. The validator assumes required inheritance through references. I fixed this by simply breaking apart the main schema into subschemas and only using the required subschema when necessary.

Loop through JSON from jquery getJSON

Here is the code:
$.getJSON( base_url + '/ajax/sortListings.php', { sort: sort }, function( data ) {
$.each(data, function(i, json) {
$( '#listings' ).append($('<div>').load( base_url + '/partialviews/listingAdminPrev.php', {
id: json.id,
name: json.name,
logo: encodeURIComponent(json.logo),
address: json.address,
city: json.city,
state: json.state,
zip: json.zip,
phone: json.phone,
email: json.email,
web_link: encodeURIComponent(json.web_link),
services: json.services,
category: json.category,
status: json.status,
created: json.created
} ));
});
});
When I manually go to the sortListings.php file and turn the $_POST variables to $_GET, it works fine. So nothing is wrong with the file. But here it is anyway:
include_once('../../app/scripts/config.php');
$listingObject = Listing::getInstance();
$results = $listingObject->get_listings($_POST['sort']);
echo json_encode($results);
That file returns this:
[
{
"id": "32",
"user_id": "32",
"logo": "32_52a0960ba791c.jpg",
"name": "Anthony Thomas Advertising",
"address": "380 S. Main St.",
"city": "Akron",
"state": "AL",
"zip": "44311",
"phone": "3302536888",
"email": "wayne#anthonythomas.com",
"web_link": "http://www.aol.com",
"status": "1",
"services": "dfhfdhdfh",
"category": "1",
"created": "2013-12-05 09:32:56"
},
{
"id": "20",
"user_id": "10",
"logo": "10_529f96001390d.png",
"name": "Graphic Installation Services",
"address": "2808 Broadway Blvd Unit 1",
"city": "Monroeville",
"state": "PA",
"zip": "15146",
"phone": "3306599898",
"email": "graphic#graphicinstallationservices.com",
"web_link": "",
"status": "4",
"services": "Graphic installation services",
"category": "1",
"created": "2013-12-04 11:35:11"
},
{
"id": "21",
"user_id": "10",
"logo": "10_529f9c1a8375d.png",
"name": "Intellect Productions",
"address": "2915 13th St NW",
"city": "Canton",
"state": "OH",
"zip": "44708",
"phone": "3309334833",
"email": "mouseywings#live.com",
"web_link": "",
"status": "1",
"services": "Car Wrap Installations by Intellect Productions",
"category": "1",
"created": "2013-12-04 15:20:15"
},
{
"id": "19",
"user_id": "10",
"logo": "10_529cf170b08d7.png",
"name": "International Installations Inc",
"address": "833 Wooster Rd N",
"city": "Barberton",
"state": "OH",
"zip": "44203",
"phone": "3306586526",
"email": "internationalinstallers#internationinstallers.com",
"web_link": "http://intellectproductions.com/",
"status": "1",
"services": "We install:\r\n· vehicle wraps\r\n· decals\r\n· vehicle lettering\r\n· banners\r\n· billboards\r\n· murals\r\nInternational Image Application Inc. is PDAA certified. We strive to deliver a constant flow of high quality work using best materials in the business, and unsurpassed skill. This ensures that our clients receive value for money, and more bang for their buck!\r\nAnd to ensure that our clients continue to receive the highest quality of work possible, we stay on top of technological trends, new materials, and installation techniques. This dedication to continued education has resulted in many clients coming back again and again.\r\nFrom a simple vehicle wraps installation to an entire ad campaign or fleet, International Image Application Inc. is your destination for precision graphics installation on virtually any medium.",
"category": "1",
"created": "2013-12-04 10:32:52"
}
]
Which is 5 listings.. so it's pulling the data fine. However, nothing is working beyond the $.each().. I even tried alerting stuff and nothing.
No console errors are thrown either..
You hit on your problem. You are expecting the sort parameter in $_POST while you are calling getJSON() which executes a GET.
Make up your mind whether you want to use GET or POST and be consistent.
My guess is you also need to put better error handling in Listing::get_listings() method to provide useful errors when you are not getting the data your expect passed to it.

bryntum component integration

I am trying to integrate the bryntum component(schedule) in php. I am not much aware in ext js.
Please see the images here
Here, Name fields are fetching properly, whereas Capacity is not accessing. These values are coming from Zoho CRM.
My code is like Click, whereas r-read.php file is the responsible file for fetching the record from CRM and store it in a json format. It is like
{
"success": true,
"total": 9,
"root": [{
"Id": 1,
"Name": "Sri Test",
"Capicity": "190.0"
}, {
"Id": 2,
"Name": "tester_test01",
"Capicity": "500.0"
}, {
"Id": 3,
"Name": "Tesing room 23",
"Capicity": "5000.0"
}, {
"Id": 4,
"Name": "Test for 6th product",
"Capicity": "5000.0"
}, {
"Id": 5,
"Name": "Banquet hall test-01",
"Capicity": "500.0"
}, {
"Id": 6,
"Name": "test room",
"Capicity": "1000.0"
}, {
"Id": 7,
"Name": "Grande Ballroom",
"Capicity": "4000.0"
}, {
"Id": 8,
"Name": "Cedar Room",
"Capicity": "1400.0"
}, {
"Id": 9,
"Name": "Maple Room",
"Capicity": "1200.0"
}]
}
In the capacity column, it will show like 190.0 , 500.0, 5000.0 etc like Name column.
I'm not familier with the Bryntum schedular component, but most of the time when you have problems like these it's because you didn't define the Capacity field in your model.
I saw you used the following model: Sch.model.Resource. Can it be that is only has the Name field and not Capacity? Your JSON response looks fine to me.
In the sample JSON above, Capacity is spelled Capicity.
See if the same spelling needs can be used everywhere. Maybe then the data will resolve properly.

Categories