JSON data modeling: users who have access to projects - json

When structuring our data model (JSON in a document database), we have users which are part of a project. I.E. a project has many users and a user can be part of many projects.
Does it make sense to store each project a user has access to in the user:
{
"id": "u1"
"name": "John Doe",
"email": "john.doe#domain.com",
"projects": [
"p1",
"p2",
"p3"
]
}
Or instead store the users in the projects themselve:
{
"id": "p1"
"name": "Project A",
"users": [
"u1"
]
}
{
"id": "p2"
"name": "Project B",
"users": [
"u1"
]
}
{
"id": "p3"
"name": "Project C",
"users": [
"u1"
]
}
We anticipate needing to select all projects for a given user as well as selecting all users for a given project.

According to document of Mongo database (https://docs.mongodb.com/manual/tutorial/model-embedded-one-to-many-relationships-between-documents), two ways above is fine.
I think the business will make decision that which data model should be chosen, for e.g, if there were only one or two projects but many users and you only need to show list of projects of users, the first way is better.
P/S: If you use Mongo, please consider to use mongoose which helps to retrieve data easily.

Related

what is the best way to manage nested data in order to have a persistent angular application?

I'm looking for advice on the best way to organize the way I manage data in an angular 14 application. Basically, this web application consist in groups of forms through several pages that are nested in three levels of information as follows :
Parent form : Intervention x (Application can have several interventions as this one)
Child form : Enterprise y (One intervention can have several enterprises)
Child form : Employee z (several individuals associated to an enterprise)
Each form information is collected using reactive forms through different pages that are shown in the screen thanks to router-navigation module of Angular.
What have I tried so far:
I started developing the application from the individual level, and then, I share the information between the pages using a service called on the component of each page. Later, I created the enterprise forms and another service specifically called to store their information.
In order to make the data persistent, I am currently using json-server to store the data in a .json file and simulate an API behavior. This allows me to do Http calls from the services in order to get, put and post the Data in the .json.
Is there a better way?
Right now I'm developing the intervention form, it is becoming very hard to maintain the child forms and I guess there is a better way to manage data for this kind of applications.
Here an example of the .json server structure :
{"interventions":
[{
"id": 1,
"user": "",
"email": "",
"enterprises":
[{
"id": 1,
"name": "",
"address": "",
"employees":
[{
"id": 1,
"name": "",
"email": ""
},
{
"id": 2,
"user": "",
"email": ""
}]
},
{
"id": 2,
"user": "",
"email": "",
"address": "yyy",
}]
},
{
"id": 2,
"user": "",
"email": "",
"address": "yyy",
}]
}
Hope this question is clear, I can provide more details if needed.
Thank you in advance.

replace "key" name in whole JSON python for bulk data in efficient way

Actually i am pushing data to other system but before pushing i have to change the "key" in the whole JSON. JSON may contain 200 or 10000 or 250000 data.
sample JSON:
{
"insert": "table",
"contacts": [
{
"testName": "testname",
"ContactID": 212121
},
{
"testName": "testname",
"ContactID": 2146354564
},
{
"testName": "testname",
"ContactID": 12312
},
{
"testName": "testname",
"ContactID": 211221
},
{
"testName": "testname",
"ContactID": 10218550
}
]
}
I need to change contacts array Keys. These contacts may be in bulk. So i need to work with this efficiently with minimal complexity.
The above JSON to be converted as below
{
"insert": "table",
"contacts": [
{
"name": "testname",
"phone": 212121
},
{
"name": "testname",
"phone": 2146354564
},
{
"name": "testname",
"phone": 12312
},
{
"name": "testname",
"phone": 211221
},
{
"name": "testname",
"phone": 10218550
}
]
}
here is my code trying by loop
ini_dict = request.data
contact_data = ini_dict['contacts']
for i in contact_data:
i['name'] = i.pop('testName')
print(contact_data)
Please suggest me how can i change the key names efficiently for bulk data. i mean for 50000 lists in contacts. "for loop" will be leading a performance issue. So please let me know the efficient way to achieve this
I dont know how fast you need it to be nor how you are choosing to store your json. One simple solution is just store it as a string and then replace all the instances of your attributes.
# Something like this using a jsonstring
jsonstring.replace("'testName':", "'name':")
jsonstring.replace("'ContactId':", "'phone':")
If you want to do this in bulk you, may need to create some batch process to be able to fetch multiple existing records and make changes at once. I have done this before with the java equivalent of https://pypi.org/project/JayDeBeApi/ but, that was more for modifying existing records in a database.

Search parameter not working with the API request

I am new to json and APIs.
This particular dataset I am working with (https://api.cdnjs.com/libraries) is searchable by placing ?search=search_term after it (like: https://api.cdnjs.com/libraries?search=cloud). But when I use another json dataset (http://api.nobelprize.org/v1/prize.json for instance) it gives an error:
{ "error":"Using an unknown parameter" }
Does this have to do with how the datasets are set up? Methods perhaps? How do I make the other json searchable like the initial one (so I can use it in my search app)?
Navigating to the main domain of the provided url, you will find that the doc reside at https://nobelprize.readme.io/v1.0
To refine your results, as the docs suggest you should use the following parameters (when searching for prizes): year, yearTo, category and numberOfLaureates.
Here is an example of a refined search:
Request url: http://api.nobelprize.org/v1/prize.json?category=physics&year=2017
Response:
{
"prizes": [{
"year": "2017",
"category": "physics",
"laureates": [{
"id": "941",
"firstname": "Rainer",
"surname": "Weiss",
"motivation": "\"for decisive contributions to the LIGO detector and the observation of gravitational waves\"",
"share": "2"
}, {
"id": "942",
"firstname": "Barry C.",
"surname": "Barish",
"motivation": "\"for decisive contributions to the LIGO detector and the observation of gravitational waves\"",
"share": "4"
}, {
"id": "943",
"firstname": "Kip S.",
"surname": "Thorne",
"motivation": "\"for decisive contributions to the LIGO detector and the observation of gravitational waves\"",
"share": "4"
}]
}]
}
If it wasn't clear enough, I made a request to the API to retrieve data concerning prizes awarded in the physics category in the year 2017.

How to get structured json from two tables using REST request

Is it capable to implement feature of procedure on getting structured response from two tables
I have two tables: 'Company' and 'Phone'
Company contains companies and Phone contains phone numbers on each company.
One company --> many phones related by companyID
I need exactly this structure:
{
"resource": [
{
"companyId": 1,
"companyName": "Apple",
"companySubject": "Security",
"phones": [
{
"name": "tel1",
"phone": "+178548555"
}
]
},
{
"companyId": 2,
"companyName": "Samsung",
"companySubject": "IT",
"phones": [
{
"name": "tel1",
"phone": "+178548555"
},
{
"name": "tel2",
"phone": "+178548555"
},
{
"name": "tel3",
"phone": "+178548555"
}
]
},
{
"companyId": 3,
"companyName": "LG",
"companySubject": "Accounting",
"phones": [
{
"name": "tel1",
"phone": "+178548555"
},
{
"name": "tel2",
"phone": "+178548555"
}
]
}
]
}
Problem is that they are in separate tables and I cant figure out how to implement it.
I use dreamfactory for backend
If the tables have a foreign key relationship (defined in the database) then you can simply fetch the phones by calling GET {service_name}/_table/{table_name}?related={relationship_name}
In your example above this might be something like GET my_service/_table/Company?related=phones_by_companyId
You can get the relationship name by looking at the table structure information in the DreamFactory Schema tab, and you can even create an alias for this relationship so that the relationship name would just be phones. Working with related data
If the tables are not related you can use DreamFactory's virtual foreign keys to relate them. More info about VFK's can be found on the wiki

Dealing with tree-like data structures in JSON API

Let's say we have several resources, that can exist by themselves or can be organized in a tree-like hierarchy. I called them roots, branches and leafs just for convenience. Now I want to retrieve leaf's data:
GET /leaf/1
Accept: application/vnd.api+json
which must return me something like this according to JSON API spec:
{
"data": [{
"type": "leaf",
"id": "1",
"title": "Leaf 1",
"links": {
"self": "http://example.com/leaf/1",
"branch": {
"self": "http://example.com/leaf/1/links/branch",
"related": "http://example.com/leaf/1/branch",
"linkage": { "type": "branch", "id": "5" }
},
"root": {
"self": "http://example.com/leaf/1/links/root",
"related": "http://example.com/leaf/1/root",
"linkage": { "type": "root", "id": "7" }
}
}
}],
"included": [{
"type": "branch",
"id": "5",
"title": "Branch 5",
"links": {
"self": "http://example.com/branch/5"
}
}, {
"type": "root",
"id": "7",
"title": "Root 7",
"links": {
"self": "http://example.com/root/7"
}
}]
}
From the response data I can't say that the data is hierarchical and it seems appropriate to change leaf's root:
PATCH /leaf/1
Content-Type: application/vnd.api+json
{
"data": {
"type": "leaf",
"id": 1,
"links": {
"root": {
"linkage": { "type": "root", "id": "3"}
}
}
}
}
Which is of course not possible because this leaf is connected to the branch and only then to the root, and changing the root in the schema below requires to have an id of this root's branch. Questions are:
How (if it's possible) to represent a hierarchy in the resource
representation to make it clear to an API user that the changing
relationships may require additional data?
The problem with your example is that you are viewing your data as leafs and branches, not separate objects.
REST is about managing entities. Try to look at REST URL as a name of the entity. Not some fancy tree pointer.
So, in your case PATCHing leafs is not possible like that. To change an entity you must use the same path you used to create it.
I know this answer is late, but I'm just leaving it here for others in the future.
I think the way to go is to replace the "branch" and "root" relationships with a single "parent" relationship that points to the parent node (which is of course on a branch under some root). Then, the parent relationship can be patched to any other node, without an inconsistency being introduced (since that was the problem before with someone patching root but not branch).
Then, if you still want to expose the root node, not just the direct parent, you could put this information in meta somewhere, you could add back a "root" relationship but not make it writeable (I.e. PATCH attempts would return 400/403), etc.